我建立使用反射来构建序列化的数据序列化成分,但我从列举的特性越来越怪异的结果: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