我有以下代码:
PropertyInfo[] originalProperties = myType.GetProperties();
我想从originalProperties
所有索引器中排除(myVar["key"] 显示为名为“Item”的属性)。
什么是正确的方法?
排除所有不能选择的属性propInfo.Name == "Item"
。
我有以下代码:
PropertyInfo[] originalProperties = myType.GetProperties();
我想从originalProperties
所有索引器中排除(myVar["key"] 显示为名为“Item”的属性)。
什么是正确的方法?
排除所有不能选择的属性propInfo.Name == "Item"
。
调用PropertyInfo.GetIndexParameters
- 如果返回的数组为空,则它不是索引器。
另一种选择是使用:
myType.GetProperties().Except(myType.GetDefaultMembers().OfType<PropertyInfo>());
GetDefaultMembers
将返回所有编译器生成的 C# 索引器。这样做的好处是不需要反映每个单独的属性来找出哪些是索引器。
这可能不是所有允许的 .NET 框架语言的通用解决方案,但我目前不知道有任何反例。