假设以下定义:
[Flags]
enum Days {
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
[Flags]
enum WeekOfMonth {
First = 256,
Second = 512,
Third = 1024,
Fourth = 2048
}
您可以使用以下代码获取所需的字符串:
var value = (Int32) WeekOfMonth.Second + (Int32) Days.Monday; // 514
var days = (Days) (value & 0xFF);
var weekOfMonth = (WeekOfMonth) (value & 0xFF00);
var str = String.Format("{0} & {1}", weekOfMonth, days);
该变量str
将Second & Monday
根据需要包含。
[Flags]
如果您希望能够组合多个Days
值或值,则该属性很重要WeekOfMonth
。例如((Days) 3).ToString()
将返回Sunday, Monday
. 如果您忘记[Flags]
返回的字符串的属性是3
.