“未找到属性设置方法”反射时出错
我试图反映一些类属性并以编程方式设置它们,但看起来我的 PropertyInfo 过滤器之一不起作用:
//Get all public or private non-static properties declared in this class (no inherited properties) - that have a getter and setter.
PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty );
我收到一条错误消息,
pi.SetValue(this, valueFromData, null);
因为该属性只有一个 get{}
方法,没有 set{}
方法。
我的问题是,为什么这个属性没有从 props 中过滤掉?我认为这就是 BindingFlags.SetProperty 的目的。
未过滤掉的属性是:
public String CollTypeDescription
{
get { return _CollTypeDescription; }
}
请注意,我想过滤无法提前工作的属性,因为我一次列出了所有属性。我不想在事后使用 pi.GetSetMethod() 来确定是否可以使用设置器。
I'm trying to reflect over some class properties and set them programmatically, but it looks like one of my PropertyInfo filters isn't working:
//Get all public or private non-static properties declared in this class (no inherited properties) - that have a getter and setter.
PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty );
I'm getting an error on the line
pi.SetValue(this, valueFromData, null);
Because the property has only a get{}
method, no set{}
method.
My question is, why wasn't this property filtered out of props? I thought that was the purpose of BindingFlags.SetProperty.
The property not getting filtered out is:
public String CollTypeDescription
{
get { return _CollTypeDescription; }
}
Note that I want to filter properties that won't work ahead of time because I'm listing them all at once. I don't want to use pi.GetSetMethod()
after the fact to determine whether I can use the setter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从文档中:
BindingFlags.SetProperty
和BindingFlags.GetProperty
不分别过滤缺少 setter 或 getter 的属性。要检查是否可以设置属性,请使用
CanWrite
属性。From the documentation:
BindingFlags.SetProperty
andBindingFlags.GetProperty
do not filter properties that are missing setters or getters, respectively.To check if a property can be set, use the
CanWrite
property.感谢肯提供信息。这看起来是最好的解决方案,我可以通过在 LINQ 过滤器中测试 GetSetMethod(true) 来过滤掉它们:
或者,使用
CanRead
和CanWrite
:我不清楚对于 get/set 方法的不同保护级别,这些不同的方法是否会产生不同的结果。
Thanks to ken for the information. It looks like the best solution I can get it to filter them out by testing GetSetMethod(true) in a LINQ filter:
Alternatively, using
CanRead
andCanWrite
:It's unclear to me whether these different approaches will yield different results for different protection levels of the get/set methods.
我使用这段代码:
I use this code:
我了解 GetProperties() 方法,以便它返回具有
BindingFlags.GetProperty
或BindingFlags.SetProperty
的每个属性。因此,如果您只需要具有设置器的属性,则必须删除
BindingFlags.GetProperty
标志。但我没有测试过,所以我可能是错的。我的答案是-1。所以看来我的回答是错误的。
I understand the GetProperties() method so that it returns every property that has
BindingFlags.GetProperty
orBindingFlags.SetProperty
.So if you want only properties that have setters you must remove the
BindingFlags.GetProperty
flag. But I did not tested it so I can be wrong.My answer got a -1. So it seems that my answer is wrong.