谁能看到我下面做错了什么?该类型具有服务方法试图访问的公共属性,为什么它不被反射拾取?
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
}
}
Run Code Online (Sandbox Code Playgroud)
干杯
斯图尔特
SomeProperty - 顾名思义 - 是一个属性.使用GetProperty和GetProperties,而不是!这导致PropertyInfo而不是FieldInfo.
| 归档时间: |
|
| 查看次数: |
1323 次 |
| 最近记录: |