-
I’ve asked a quesion earlier which can be found here,
and I was wondering whether the accepted answer’s code can be done in normal programming withoutLinqquery, any example will be very appreciated.
Note that this is the way I’ve started to implement it, and had some problem through implementation. -
I’ve managed to get private fields value through reflection without working with properties, is it problematic? Should I always take values through properties in reflection?
Code:
public static class Extensions
{
public static XElement ToXml<T>(this T obj)
{
Type type = typeof(T);
return new XElement("Class",
new XElement(type.Name,
from pi in type.GetProperties()
where !pi.GetIndexParameters().Any()
let value = (dynamic)pi.GetValue(obj, null)
select pi.PropertyType.IsPrimitive ||
pi.PropertyType == typeof(string) ?
new XElement(pi.Name, value) :
Extensions.ToXml(value)
)
);
}
}
Thanks in advance
If you don’t want to use LINQ:
Usage: