使用反射从简单的类中获取字段值

发布于 2024-12-07 17:43:49 字数 456 浏览 3 评论 0原文

我有一个类:

class A {
    public string a = "A-val" , b = "B-val";
}

我想通过反射打印对象成员

//Object here is necessary.
Object data = new A();
FieldInfo[] fields = data.GetType().GetFields();
String str = "";
foreach(FieldInfo f in fields){
    str += f.Name + " = " + f.GetValue(data) + "\r\n";
}

这是所需的结果:

a = A-val
b = B-val

不幸的是,这不起作用。请帮忙,谢谢。

I have a class:

class A {
    public string a = "A-val" , b = "B-val";
}

I want to print the object members by reflection

//Object here is necessary.
Object data = new A();
FieldInfo[] fields = data.GetType().GetFields();
String str = "";
foreach(FieldInfo f in fields){
    str += f.Name + " = " + f.GetValue(data) + "\r\n";
}

Here is the desired result:

a = A-val
b = B-val

Unfortunately this did not work. Please help, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒中人 2024-12-14 17:43:49

一旦修复并消除错误(缺少分号和错误的变量名称),您发布的代码确实可以工作 - 我刚刚尝试过,它显示了名称和值没有问题。

我的猜测是,实际上,您正在尝试使用非公开的字段。此代码:

FieldInfo[] fields = data.GetType().GetFields();

...只会获取公共字段。您通常需要指定您还需要非公共字段:(

FieldInfo[] fields = data.GetType().GetFields(BindingFlags.Public | 
                                              BindingFlags.NonPublic | 
                                              BindingFlags.Instance);

我希望您实际上没有公共字段,毕竟......)

Once fixed to get rid of the errors (lacking a semi-colon and a bad variable name), the code you've posted does work - I've just tried it and it showed the names and values with no problems.

My guess is that in reality, you're trying to use fields which aren't public. This code:

FieldInfo[] fields = data.GetType().GetFields();

... will only get public fields. You would normally need to specify that you also want non-public fields:

FieldInfo[] fields = data.GetType().GetFields(BindingFlags.Public | 
                                              BindingFlags.NonPublic | 
                                              BindingFlags.Instance);

(I hope you don't really have public fields, after all...)

独木成林 2024-12-14 17:43:49

请记住,当您编写如下字段时:

public string VarName{ get; set;}

那么实际上您有以下代码(这就是反射看到的内容):

private string _varName;
public string get_VarName(){
....
}
public void set_VarName(strig value){
....
}

Remember when you write fields like :

public string VarName{ get; set;}

Then actually you have this code(this is what reflection see) :

private string _varName;
public string get_VarName(){
....
}
public void set_VarName(strig value){
....
}
时光礼记 2024-12-14 17:43:49

正如@Stanislav 所说,您必须记住编译器为属性生成的支持字段。如果您想排除这些字段,可以使用以下代码:

FieldInfo[] fields = data.GetType()
    .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
    .Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
    .ToArray();

As @Stanislav say, you must keep in mind the backing fields generated by the compiler for properties. If you want to exclude these fields you can use the following code:

FieldInfo[] fields = data.GetType()
    .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
    .Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
    .ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文