![](/img/trans.png)
[英]Method GetProperties with BindingFlags.Public doesn't return anything
[英]Why is my Type.GetFields(BindingFlags.Instance|BindingFlags.Public) not working?
我的代码可以看到非公开成员,但不能看到公共成员。 为什么?
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;
}
}
也许是因为你正在使用GetFields
并且该类没有任何公共字段:属性和字段是两个不同的东西。
尝试使用GetProperties
方法而不是GetFields
。
一般来说,你不会找到任何非公共领域 。 假设你的典型类的结构是这样的:
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.