PropertyInfo : is the property an indexer?

前端 未结 2 1320
小鲜肉
小鲜肉 2020-12-17 07:35

I have the following code :

PropertyInfo[] originalProperties = myType.GetProperties();

I want to exclude from originalProperties

相关标签:
2条回答
  • 2020-12-17 08:17

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

    0 讨论(0)
  • 2020-12-17 08:23

    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 讨论(0)
提交回复
热议问题