Hello, look at the following example:
public class Test {
public int Number { get; set; }
public void TestReflection() {
Number = 99;
Type type = GetType();
PropertyInfo propertyInfo = type.GetProperty("Number");
propertyInfo.SetValue(this, null, null);
}
}
In the example I'm setting a int
property to null
using reflection. I was expecting this to throw an exception because null
isn't a valid value for int
. But it doesn't throw, it just sets the property to 0. Why!?
Update
Ok, it seems that is just how it is. The property gets the default value of the value-type if you try to set it to null. I have posted an answer describing how I solved my problem, maybe that will help someone someday. Thanks to all who answered.