在C#中使用反射识别自定义索引器 [英] Identifying a custom indexer using reflection in C#

查看:455
本文介绍了在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()?

推荐答案

您还可以使用 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天全站免登陆