代码之家  ›  专栏  ›  技术社区  ›  granadaCoder

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

  •  4
  • granadaCoder  · 技术社区  · 14 年前

    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 回复  |  直到 14 年前
        1
  •  10
  •   Dan Tao    14 年前

    一般来说你不会发现 任何 非公开 领域

    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
    )
    
        2
  •  11
  •   LukeH    14 年前

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

    尝试使用 GetProperties 方法而不是 获取字段 .