Determine if reflected property can be assigned null

后端 未结 4 750
灰色年华
灰色年华 2020-12-15 16:15

I wish to automagically discover some information on a provided class to do something akin to form entry. Specifically I am using reflection to return a PropertyInfo value f

4条回答
  •  无人及你
    2020-12-15 16:41

    PropertyInfo propertyInfo = ...
    bool canAssignNull = 
        !propertyInfo.PropertyType.IsValueType || 
        propertyInfo.PropertyType.IsGenericType &&
            propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
    

提交回复
热议问题