c# - Getting the default of an unknown type at runtime -
this question has reply here:
how can phone call default(t) type? [duplicate] 3 answersi using reflection properties of object. need see if of property’s values default of whatever type happen be. below current code. complaining namespace or type not found. leads me believe has how c# implicit type coercion. since grabbing type @ run time not know how compare or not clear on that.
i hoping avoid switch case comparing on name of type comes in right looking alternative unless brilliant people on stackoverflow can lead me in right direction.
private bool testpropertyattribute(propertyinfo prop) { dynamic value = prop.getvalue(dataobject, null); type type = prop.propertytype; /* test see if value defult of type */ homecoming (value == default(prop.propertytype) }
== object mean: reference equality. reference, default null, if !prop.propertytype.isvaluetype, need null check. value-types, you boxing. reference equality study false, unless both nullable<t> t, , both empty. however, "default" value-type (prop.propertytype.isvaluetype), can utilize activator.createinstance(prop.propertytype). maintain in mind == not going want here. equals(x,y) might work better.
c# reflection types type-conversion type-coercion
No comments:
Post a Comment