问题描述
当我在抽象类中拥有此属性时:
When I have this property in an abstract class:
public IList<Component> Components { get; private set; }
然后我打电话:
p.GetSetMethod(true)
p是一个指向我的属性的PropertyInfo对象,我得到null.
with p being a PropertyInfo object pointing to my property, I get null.
但是,如果我将属性设置器更改为protected,则可以通过反射看到它.为什么是这样?我似乎不记得非抽象类有这个问题...
However if I change the property setter to protected, I can see it via reflection. Why is this? I don't seem to recall having this problem with non-abstract classes...
推荐答案
我假设您正在从抽象类的派生类型的对象上调用此方法.该类上根本没有属性设置器.它仅位于您的抽象库上.这就是为什么当您将其标记为受受保护的
时它可以工作的原因.获取属性设置器时,您需要使用抽象类的 Type
.
I assume you are calling this on an object from a derived type of your abstract class. There isn't a property setter at all on that class. It is only located on your abstract base. This is why it works when you mark it as protected
. You need use your abstract class' Type
instead when getting the property setter.
这篇关于反思找不到抽象类属性的私有设置者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!