我正在构建一个使用反射来构建序列化数据的序列化组件,但是我从枚举属性中得到了奇怪的结果:
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 获取变量的真实值,即使它是一个枚举并且只有一个值?
谢谢