为什么我的Type.GetFields(BindingFlags.Instance | BindingFlags.Public)不起作用?

时间:2010-06-11 15:48:43

标签: c# .net reflection

我的代码可以看到非公开成员,但不能看到公共成员。为什么呢?

FieldInfo[] publicFieldInfos =
    t.GetFields(BindingFlags.Instance | BindingFlags.Public);

什么也没有回来。

注意:我正在尝试获取抽象类的属性以及 具体的课程。 (并阅读属性)。

MSDN示例使用2个标志(BindingFlags.Instance | BindingFlags.Public),但下面的迷你继承示例没有。

private void RunTest1()
{
    try
    {
        textBox1.Text = string.Empty;

        Type t = typeof(MyInheritedClass);

        //Look at the BindingFlags *** NonPublic ***

        int fieldCount = 0;

        while (null != t)
        {
            fieldCount += t.GetFields(BindingFlags.Instance |
            BindingFlags.NonPublic).Length;

            FieldInfo[] nonPublicFieldInfos = t.GetFields(BindingFlags.Instance |
            BindingFlags.NonPublic);

            foreach (FieldInfo field in nonPublicFieldInfos)
            {
                if (null != field)
                {
                    Console.WriteLine(field.Name);
                }
            }

            t = t.BaseType;
        }

        Console.WriteLine("\n\r------------------\n\r");

        //Look at the BindingFlags *** Public ***

        t = typeof(MyInheritedClass);

        FieldInfo[] publicFieldInfos = t.GetFields(BindingFlags.Instance |
        BindingFlags.Public);

        foreach (FieldInfo field in publicFieldInfos)
        {
            if (null != field)
            {
                Console.WriteLine(field.Name);

                object[] attributes = field.GetCustomAttributes(t, true);

                if (attributes != null && attributes.Length > 0)
                {
                    foreach (Attribute att in attributes)
                    {
                        Console.WriteLine(att.GetType().Name);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        ReportException(ex);
    }
}

private void ReportException(Exception ex)
{
    Exception innerException = ex;

    while (innerException != null)
    {
        Console.WriteLine(innerException.Message + System.Environment.NewLine +
        innerException.StackTrace + System.Environment.NewLine +
        System.Environment.NewLine);

        innerException = innerException.InnerException;
    }
}

public abstract class MySuperType
{
    public MySuperType(string st)
    {
        this.STString = st;
    }

    public string STString
    {
        get;
        set;
    }

    public abstract string MyAbstractString { get; set; }
}

public class MyInheritedClass : MySuperType
{
    public MyInheritedClass(string ic)
        : base(ic)
    {
        this.ICString = ic;
    }

    [Description("This is an important property"), Category("HowImportant")]
    public string ICString
    {
        get;
        set;
    }

    private string _oldSchoolPropertyString = string.Empty;

    public string OldSchoolPropertyString
    {
        get { return _oldSchoolPropertyString; }
        set { _oldSchoolPropertyString = value; }
    }

    [Description("This is a not so importarnt property"),
    Category("HowImportant")]
    public override string MyAbstractString
    {
        get;
        set;
    }
}

编辑

在我接受这里给出的建议之后,这是我的代码:

private void RunTest1()
{
    try
    {

        textBox1.Text = string.Empty;

        Type t = typeof(MyInheritedClass);


        //Look at the BindingFlags *** NonPublic ***
        int fieldCount = 0;
        while (null != t)
        {
            fieldCount += t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Length;

            PropertyInfo[] nonPublicFieldInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (PropertyInfo field in nonPublicFieldInfos)
            {
                if (null != field)
                {
                    Console.WriteLine(field.Name);

                }
            }

            t = t.BaseType;

        }



        Console.WriteLine("\n\r------------------\n\r");



        //Look at the BindingFlags *** Public ***
        t = typeof(MyInheritedClass);
        PropertyInfo[] publicFieldInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);

        foreach (PropertyInfo field in publicFieldInfos)
        {
            if (null != field)
            {
                Console.WriteLine(field.Name);
                textBox1.Text += field.Name + System.Environment.NewLine;
                DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);             

                if (attributes != null && attributes.Length > 0)
                {
                    foreach (Attribute att in attributes)
                    {
                        Console.WriteLine(att.GetType().Name);

                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        ReportException(ex);
    }

}

private void ReportException(Exception ex)
{



    Exception innerException = ex;
    while (innerException != null)
    {
        Console.WriteLine(innerException.Message + System.Environment.NewLine + innerException.StackTrace + System.Environment.NewLine + System.Environment.NewLine);

    }

}


public abstract class MySuperType
{
    public MySuperType(string st)
    {
        this.STString = st;
    }
    public string STString
    {
        get;
        set;
    }

    public abstract string MyAbstractString {get;set;}

}

public class MyInheritedClass : MySuperType
{
    public MyInheritedClass(string ic)
        : base(ic)
    {
        this.ICString = ic;
    }

    [Description("This is an important property"),Category("HowImportant")]
    public string ICString
    {
        get;
        set;
    }


    private string _oldSchoolPropertyString = string.Empty;
    public string OldSchoolPropertyString
    {
        get { return _oldSchoolPropertyString; }
        set { _oldSchoolPropertyString = value; }
    }


    [Description("This is a not so importarnt property"), Category("HowImportant")]
    public override string MyAbstractString
    {
        get; set;
    }


}

2 个答案:

答案 0 :(得分:11)

也许是因为你正在使用GetFields并且该类没有任何公共字段:属性和字段是两个不同的东西。

尝试使用GetProperties方法代替GetFields

答案 1 :(得分:7)

通常,您不会找到任何非公开字段。假设你的典型类是这样的结构:

public class MyClass {
    private int myField;
    public int MyProperty {
        get { return myField; }
    }
}

请注意字段myField)是私有的,而属性MyProperty)是公开的。

因此,要查找字段,您可能会获得最大的收益:

// note: fields -> generally non-public
Type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)

而对于属性而言则相反:您可能会获得最大的收益:

// note: properties -> generally public
Type.GetProperties(BindingFlags.Instance | BindingFlags.Public)

显然,如果你想找到特定种类(字段/属性)的所有(公共非公开)成员,那么你将不得不使用方法:

Type.GetFields( // (or GetProperties)
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
)