Identifying a custom indexer using reflection in C#

前端 未结 5 1154
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 16:42

I have a class with a custom indexer like so

public string this[VehicleProperty property]
{
  // Code
}

How can I identify the custom index

5条回答
  •  一整个雨季
    2020-12-03 17:30

    You can also look for index parameters, using the the PropertyInfo.GetIndexParameters method, if it returns more than 0 items, it's an indexed property:

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

提交回复
热议问题