使用C#中的反射识别自定义索引器

jbe*_*ert 29 .net c#

我有一个类似自定义索引器的类

public string this[VehicleProperty property]
{
  // Code
}
Run Code Online (Sandbox Code Playgroud)

如何在typeof(MyClass).GetProperties()的结果中识别自定义索引器?

CMS*_*CMS 42

您还可以使用PropertyInfo.GetIndexParameters方法查找索引参数,如果它返回的项目超过0,则它是一个索引属性:

foreach (PropertyInfo pi in typeof(MyClass).GetProperties())
{
    if (pi.GetIndexParameters().Length > 0)
    {
       // Indexed property...
    }
}
Run Code Online (Sandbox Code Playgroud)


lep*_*pie 5

查找DefaultMemberAttribute类型级别的已定义.

(过去曾经如此IndexerNameAttribute,但它们似乎已经放弃了它)