PropertyInfo SetValue and nulls
Solution 1
If you have the PropertyInfo
, you can check the .PropertyType
; if .IsValueType
is true, and if Nullable.GetUnderlyingType(property.PropertyType)
is null, then it is a non-nullable value-type:
if (value == null && property.PropertyType.IsValueType &&
Nullable.GetUnderlyingType(property.PropertyType) == null)
{
throw new InvalidCastException ();
}
Solution 2
You can use PropertyInfo.PropertyType.IsAssignableFrom(value.GetType()) expression to determine whether specified value can be written into property. But you need to handle case when value is null, so in this case you can assign it to property only if property type is nullable or property type is reference type:
public bool CanAssignValueToProperty(PropertyInfo propertyInfo, object value)
{
if (value == null)
return Nullable.GetUnderlyingType(propertyInfo.PropertyType) != null ||
!propertyInfo.IsValueType;
else
return propertyInfo.PropertyType.IsAssignableFrom(value.GetType());
}
Also, you may find useful Convert.ChangeType method to write convertible values to property.
Nelson Rothermel
Updated on July 19, 2022Comments
-
Nelson Rothermel almost 2 years
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 to0
, even thoughvalue = null
. It appears it's doing something likeIntProperty = default(typeof(int))
. I would like to throw anInvalidCastException
ifIntProperty
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? -
Nelson Rothermel almost 14 yearsThat's it. I was messing with .PropertyType.IsClass, but wasn't getting very far.
-
Nelson Rothermel almost 14 yearsSetValue() already throws an exception when it can't set the value, which is desired behavior (but it's an ArgumentException). I only need to handle the null scenario.