开发者

PropertyInfo : is the property an indexer?

开发者 https://www.devze.com 2023-02-06 05:38 出处:网络
I have the following code : PropertyInfo[] originalProperties = myType.GetProperties(); I want to exclude from originalProperties all the indexers (myVar[\"key\"] appears as property named \"Item\"

I have the following code :

PropertyInfo[] originalProperties = myType.GetProperties();

I want to exclude from originalProperties all the indexers (myVar["key"] appears as property named "Item").

开发者_Go百科

What is the proper way ?

Exclude all properties where propInfo.Name == "Item" is not an option.


Call PropertyInfo.GetIndexParameters - if the returned array is empty, it's not an indexer.


Another option is to use:

myType.GetProperties().Except(myType.GetDefaultMembers().OfType<PropertyInfo>());

GetDefaultMembers will return all the compiler generated indexers in C#. This has the advantage of not needing to reflect on each individual property in order to find out which ones are indexers.

This might not be a general solution for all allowed .NET framework languages, but I am currently not aware of any counter-examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消