Find out if a property is declared virtual

前端 未结 5 1645
北荒
北荒 2020-12-25 10:53

Sorry, I am looking up the System.Type type and the PropertyInfo type in the documentation but I can\'t seem to find the thing I need.

How

5条回答
  •  渐次进展
    2020-12-25 11:34

    IsVirtual alone didn't work for me. It was telling me that all my non-virtual non-nullable properties were virtual. I had to use a combination of IsFinal and IsVirtual

    Here's what I ended up with:

    PropertyInfo[] nonVirtualProperties = myType.GetProperties().Where(x => x.GetAccessors()[0].IsFinal || !x.GetAccessors()[0].IsVirtual).ToArray();
    
    PropertyInfo[] virtualProperties = myType.GetProperties().Where(x => !x.GetAccessors()[0].IsFinal && x.GetAccessors()[0].IsVirtual).ToArray();
    

提交回复
热议问题