有人能看到我在下面做错了什么吗?该类型具有服务方法尝试访问的公共属性,那么为什么反射没有获取它呢?
Public class SomeClass
{
private YetAnotherClass yetAnotherClass;
public SomeClass(SomeOtherClass otherclass)
{
this.yetAnotherClass = otherclass.SomeProperty;
}
public YetAnotherClass SomeProperty
{
get { return this.yetAnotherClass; }
}
}
Public class ServiceClass
{
public void DoSomething(SomeClass someclass)
{
Type type = someclass.GetType();
FieldInfo[] fieldsinfo = type.GetFields(BindingFlags.Public | BindingFlags.Instance); // returns empty collection
FieldInfo fieldinfo = type.GetField("SomeProperty"); // returns null reference exception
}
}
Cheers
Stewart
顾名思义,SomeProperty 是一个属性。使用GetProperty
and GetProperties
反而!这导致PropertyInfo
代替FieldInfo
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)