I am creating a serialization component that uses reflection to build serialized data, but I get weird results from the listed properties:
enum eDayFlags { Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8, Thursday = 16, Friday = 32, Saturday = 64 } public eDayFlags DayFlags { get; set; }
Now for the real test
Obj Test = new Obj(); Test.DayFlags = eDayFlags.Friday;
Serialization Result:
DayFlags = Friday
But if I set two flags in my variable:
Obj Test = new Obj(); Test.DayFlags = eDayFlags.Friday; Test.DayFlags |= eDayFlags.Monday;
Serialization Result:
Dayflags = 34
What I'm doing in the serialization component is pretty simple:
//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())); } }
Can someone tell me how to get the real value of a variable from PropertyInfo.GetValue, even if this enumeration is only one value?
thanks
source share