![](/img/trans.png)
[英]What's wrong with this reflection code? GetFields() is returning an empty array
[英]GetFields returning empty array
任何人都可以在下面看到我在做什么錯嗎? 該類型具有服務方法嘗試訪問的公共屬性,因此為什么不通過反射來獲取它呢?
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
}
}
干杯
斯圖爾特
顧名思義,SomeProperty是一個屬性。 請改用GetProperty
和GetProperties
! 這導致了PropertyInfo
而不是FieldInfo
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.