Tôi có cờ enum sau:C# Enums - Kiểm tra Flags chống lại một Mask
[Flags]
private enum MemoryProtection: uint
{
None = 0x000,
NoAccess = 0x001,
ReadOnly = 0x002,
ReadWrite = 0x004,
WriteCopy = 0x008,
Execute = 0x010,
ExecuteRead = 0x020,
ExecuteReadWrite = 0x040,
ExecuteWriteCopy = 0x080,
Guard = 0x100,
NoCache = 0x200,
WriteCombine = 0x400,
Readable = (ReadOnly | ReadWrite | ExecuteRead | ExecuteReadWrite),
Writable = (ReadWrite | WriteCopy | ExecuteReadWrite | ExecuteWriteCopy)
}
Bây giờ tôi có một thể hiện enum mà tôi cần phải kiểm tra xem nó có thể đọc được. Nếu tôi sử dụng mã sau:
myMemoryProtection.HasFlag(MemoryProtection.Readable)
Nó luôn trả về false trong trường hợp của tôi vì tôi nghĩ HasFlag sẽ kiểm tra xem nó có gắn cờ hay không. Tôi cần một cái gì đó thanh lịch để tránh làm điều này:
myMemoryProtection.HasFlag(MemoryProtection.ReadOnly) ||
myMemoryProtection.HasFlag(MemoryProtection.ReadWrite) ||
myMemoryProtection.HasFlag(MemoryProtection.ExecuteRead) ||
myMemoryProtection.HasFlag(MemoryProtection.ExecuteReadWrite)
Tôi có thể làm như thế nào?
Yêu cầu .NET 4.0 hoặc cao hơn – linquize