Determine if reflected property can be assigned null

后端 未结 4 734
灰色年华
灰色年华 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:45

    You need to handle null references and Nullable, so (in turn):

    bool canBeNull = !type.IsValueType || (Nullable.GetUnderlyingType(type) != null);
    

    Note that IsByRef is something different, that allows you to choose between int and ref int / out int.

提交回复
热议问题