不从的GetType获取字段()。GetFields与BindingFlag.Default [英] Not getting fields from GetType().GetFields with BindingFlag.Default

查看:265
本文介绍了不从的GetType获取字段()。GetFields与BindingFlag.Default的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用顺序反射类获取某个对象内的所有领域。然而,
我的问题是,它完美的作品时,字段是一个普通类中,如:

I am using the Reflection classes in order to get all the fields inside a certain object. My problem however is that it works perfectly when the fields are inside a normal class, like:

class test
{
   string test1 = string.Empty;
   string test2 = string.Empty;
}



在这里,我得到两个TEST1和TEST2,我的问题是,我使用抽象和因此几类组合

Here i get both test1 and test2, my problem is that i use abstraction and thus several classes combined.

我是这样的:

class test3 : test2
{
   string test4 = string.Empty;
   string test5 = string.Empty;
}

class test2 : test1
{
   string test2 = string.Empty;
   string test3 = string.Empty;
}
class test1
{
   string test0 = string.Empty;
   string test1 = string.Empty;
}



但是当我运行它,我不从一开始地里回来的GetType()。GetFields(BindingFlag.Default)

这些领域的每个人也有一个属性,搞定;设置; 连接到它。
当我运行代码,我得到的属性一路回test1的但不是实际的领域。

Everyone of those fields also have a property, get; set; attached to it. When I run the code, I get the properties all the way back to test1 but not the actual fields.

这是我想要的代码获得等领域的:

This is the code that I'm trying to get the fields with:

FieldInfo[] fields = Obj.GetType().GetFields(BindingFlags.Default);
foreach (FieldInfo field in fields)



我也尝试:

I have also tried:

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



我使用的属性相同的代码:

I use the same code for the properties:

PropertyInfo[] properties = Obj.GetType().GetProperties(BindingFlags.Public 
                                             | BindingFlags.Instance 
                                             | BindingFlags.NonPublic 
                                             | BindingFlags.Static);

foreach (PropertyInfo property in properties)



任何想法,为什么我得到的从抽象类的属性而不是域

Any ideas why I get the properties from the abstracted classes but not the fields?

推荐答案

编辑:要获得的私人的成员基本类型,你必须:

To get private members of the base type, you have to:

typeof(T).BaseType.GetFields(...)

再次编辑:赢

编辑13年3月22日:使用的毗连而不是联盟。由于我们指定 BindingFlags.DeclaredOnly 和类型的 BASETYPE 不等于本身,联盟不需要,更昂贵

Edit 3/22/13: Used Concat instead of Union. Since we are specifying BindingFlags.DeclaredOnly and a type's BaseType cannot equal itself, Union is not needed and is more expensive.

public static IEnumerable<FieldInfo> GetAllFields(Type t)
{
    if (t == null)
        return Enumerable.Empty<FieldInfo>();

    BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | 
                         BindingFlags.Static | BindingFlags.Instance | 
                         BindingFlags.DeclaredOnly;
    return t.GetFields(flags).Concat(GetAllFields(t.BaseType));
}

这篇关于不从的GetType获取字段()。GetFields与BindingFlag.Default的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆