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

时间:2022-09-01 23:55:52

I have a class with a custom indexer like so

我有一个类具有自定义索引器

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

How can I identify the custom indexer in the results of typeof(MyClass).GetProperties()?

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

4 个解决方案

#1


38  

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:

您还可以使用PropertyInfo查找索引参数。GetIndexParameters方法,如果它返回大于0项,它是一个索引属性:

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

#2


4  

Look for the DefaultMemberAttribute defined at type level.

查找在类型级别定义的DefaultMemberAttribute。

(This used to be IndexerNameAttribute, but they seem to have dropped it)

(这曾经是IndexerNameAttribute,但他们似乎已经放弃了)

#3


3  

    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();
    }

#4


0  

To get a known indexer you can use:

要获得一个已知的索引器,您可以使用:

var prop = typeof(MyClass).GetProperty("Item", new object[]{typeof(VehicleProperty)});
var value = prop.GetValue(classInstance, new object[]{ theVehicle });

or you can get the getter method of the indexer:

或者你可以得到索引器的getter方法:

var getterMethod = typeof(MyClass).GetMethod("get_Item", new object[]{typeof(VehicleProperty)});
var value = getterMethod.Invoke(classInstance, new object[]{ theVehicle });

if the class has only one indexer, you can omit the type:

如果类只有一个索引器,则可以省略类型:

var prop = typeof(MyClass).GetProperty("Item", , BindingFlags.Public | BindingFlags.Instance);

I've added this answer for the ones who google search led them here.

我已经为谷歌搜索的用户添加了这个答案。

#1


38  

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:

您还可以使用PropertyInfo查找索引参数。GetIndexParameters方法,如果它返回大于0项,它是一个索引属性:

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

#2


4  

Look for the DefaultMemberAttribute defined at type level.

查找在类型级别定义的DefaultMemberAttribute。

(This used to be IndexerNameAttribute, but they seem to have dropped it)

(这曾经是IndexerNameAttribute,但他们似乎已经放弃了)

#3


3  

    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();
    }

#4


0  

To get a known indexer you can use:

要获得一个已知的索引器,您可以使用:

var prop = typeof(MyClass).GetProperty("Item", new object[]{typeof(VehicleProperty)});
var value = prop.GetValue(classInstance, new object[]{ theVehicle });

or you can get the getter method of the indexer:

或者你可以得到索引器的getter方法:

var getterMethod = typeof(MyClass).GetMethod("get_Item", new object[]{typeof(VehicleProperty)});
var value = getterMethod.Invoke(classInstance, new object[]{ theVehicle });

if the class has only one indexer, you can omit the type:

如果类只有一个索引器,则可以省略类型:

var prop = typeof(MyClass).GetProperty("Item", , BindingFlags.Public | BindingFlags.Instance);

I've added this answer for the ones who google search led them here.

我已经为谷歌搜索的用户添加了这个答案。