gpt4 book ai didi

c# - 在 C# 中使用反射识别自定义索引器

转载 作者:IT王子 更新时间:2023-10-29 04:16:19 26 4
gpt4 key购买 nike

我有一个带有自定义索引器的类

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com