I want to do some comparing of member properties at runtime but value type prohibits unusual behaviour.
I have something like the following
public static object DefaultValue(this Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
class ExampleClass
{
public string Id { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public XElement ToXml()
{
var srcTree = from prop in GetType().GetProperties()
let value = prop.GetValue(foo, null)
where value != prop.PropertyType.DefaultValue()
select new XElement(prop.Name.ToLower(), value);
...
}
}
if I initialize my object new ExampleClass()
I find the price with a value of 0. I can confirm DefaultValue()
returns 0 for the price which is not equal to 0 during equality comparison. Is because now it’s comparing objects or what. What can I achieve the behaviour I want?