Type.GetFields为公共字段返回一个空的System.Reflection.FieldInfo数组

Ale*_*lex 0 c# reflection

我有一个类只包含许多标准类型的公共字段.该调用myObject.GetType().GetFields()返回一个空数组.

问题是什么?

更新:我很抱歉,问题是我使用的是WCF服务公开的类.原始类(比如说A)和暴露(WcfReference.A)是不同的类.A的成员成为WcfReference.A中的私有字段并作为属性公开.

也许这个问题应该删除.

Rap*_*aus 5

不带参数的GetFields()将返回您的类型的公共字段(不是@Kenneth Ito注意到的属性).

public class Test {
   public string var_;//I'm a public field, I'll be returned
   private int id_; //I'm a private field, you'll have to do more to get me
   public int Id {get { return id_;} set {id_=value;}} //I'm a property, I don't feel concerned
}
Run Code Online (Sandbox Code Playgroud)

如果你这样做

var test = new Test();
test.GetType().GetFields();
Run Code Online (Sandbox Code Playgroud)

将返回一个包含一个项目的数组:var_