我建立使用反射來構建序列化的數據序列化成分,但我從列舉的特性越來越怪異的結果:PropertyInfo.GetValue返回枚舉常量的名稱,而不是價值
enum eDayFlags
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public eDayFlags DayFlags { get; set; }
現在的串行化的實際測試
Obj Test = new Obj();
Test.DayFlags = eDayFlags.Friday;
輸出則是:
DayFlags =星期五
但是,如果我設置我的變量兩個標誌:
Obj Test = new Obj();
Test.DayFlags = eDayFlags.Friday;
Test.DayFlags |= eDayFlags.Monday;
系列化的輸出則是:
DayFlags = 34
我在做什麼的序列化組件非常簡單:
//Loop each property of the object
foreach (var prop in obj.GetType().GetProperties())
{
//Get the value of the property
var x = prop.GetValue(obj, null).ToString();
//Append it to the dictionnary encoded
if (x == null)
{
Properties.Add(HttpUtility.UrlEncode(prop.Name) + "=null");
}
else
{
Properties.Add(HttpUtility.UrlEncode(prop.Name) + "=" + HttpUtility.UrlEncode(x.ToString()));
}
}
任何人都可以告訴我如何從PropertyInfo.GetValue獲取變量的實際值,即使它是一個枚舉並且只有一個值?
感謝
我建議使用十六進制或位移而不是十進制來表示標誌。低數字並不是什麼大問題,但爲了保持一致性,我會在任何地方進行。 – Brian 2012-08-14 17:45:58