蔡作馨在C#中使用反射的自定义索引 [英] Indentifying a custom indexer using reflection in C#

查看:165
本文介绍了蔡作馨在C#中使用反射的自定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类像这样一个自定义的索引

I have a class with a custom indexer like so

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

如何确定在typeof运算的结果(MyClass的).GetProperties自定义索引()?

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

推荐答案

您也可以查找指标参数,使用的<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getindexparameters.aspx">PropertyInfo.GetIndexParameters方法,如果它返回大于0的项目,它是一个索引属性:

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...
    }
}

这篇关于蔡作馨在C#中使用反射的自定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆