我有一个带有自定义索引器的类
public string this[VehicleProperty property]
{
// Code
}
如何在 typeof(MyClass).GetProperties() 的结果中识别自定义索引器?
最佳答案
您还可以查找索引参数,使用 PropertyInfo.GetIndexParameters方法,如果它返回超过 0 个项目,则它是一个索引属性:
foreach (PropertyInfo pi in typeof(MyClass).GetProperties())
{
if (pi.GetIndexParameters().Length > 0)
{
// Indexed property...
}
}
关于c# - 在 C# 中使用反射识别自定义索引器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1347936/