【发布时间】:2012-02-20 15:55:02
【问题描述】:
我正在尝试反映一些类属性并以编程方式设置它们,但我的某个 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(),事后确定我是否可以使用setter。
【问题讨论】:
-
@dtryon - 不 - 他正在对他想要设置的选项的标志进行或运算。
标签: c# .net reflection