Using reflection I need to retrieve the value of a propery of a Nullable Type of DateTime
How can I do this?
When I try propertyInfo.GetValue(object, null) it does not function.
thx
My code:
var propertyInfos = myClass.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
object propertyValue= propertyInfo.GetValue(myClass, null);
}
propertyValue result always null for nullable type
Reflection and
Nullable<T>are a bit of a pain; reflection usesobject, andNullable<T>has special boxing/unboxing rules forobject. So by the time you have anobjectit is no longer aNullable<T>– it is eithernullor the value itself.i.e.
This makes it a bit confusing sometimes, and note that you can’t get the original
Tfrom an emptyNullable<T>that has been boxed, as all you have is anull.