PropertyInfo SetValue and nulls
Posted
by Nelson
on Stack Overflow
See other posts from Stack Overflow
or by Nelson
Published on 2010-06-15T22:19:17Z
Indexed on
2010/06/15
22:22 UTC
Read the original article
Hit count: 255
If I have something like:
object value = null;
Foo foo = new Foo();
PropertyInfo property = Foo.GetProperties().Single(p => p.Name == "IntProperty");
property.SetValue(foo, value, null);
Then foo.IntProperty
gets set to 0
, even though value = null
. It appears it's doing somemething like IntProperty = default(typeof(int))
. I would like to throw an InvalidCastException
if IntProperty
is not a "nullable" type (Nullable<> or reference). I'm using Reflection, so I don't know the type ahead of time. How would I go about doing this?
© Stack Overflow or respective owner