Identifying a custom indexer using reflection in C#

前端 未结 5 1165
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  猫巷女王i
    2020-12-03 17:26

        static void Main(string[] args) {
    
            foreach (System.Reflection.PropertyInfo propertyInfo in typeof(System.Collections.ArrayList).GetProperties()) {
    
                System.Reflection.ParameterInfo[] parameterInfos = propertyInfo.GetIndexParameters();
                // then is indexer property
                if (parameterInfos.Length > 0) {
                    System.Console.WriteLine(propertyInfo.Name);
                }
            }
    
    
            System.Console.ReadKey();
        }
    

提交回复
热议问题