gra*_*der 4 .net c# reflection
我的代码可以看到非公开成员,但不能看到公共成员.为什么?
FieldInfo[] publicFieldInfos =
t.GetFields(BindingFlags.Instance | BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)
没有回来.
注意:我正在尝试获取抽象类的属性以及具体类.(并阅读属性).
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
在我接受了这里给出的建议之后,这是我的代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
一般来说,你不会找到任何非公共领域.假设你的典型类的结构是这样的:
public class MyClass {
private int myField;
public int MyProperty {
get { return myField; }
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,field(myField)是私有的,而property(MyProperty)是public.
因此,要查找字段,您可能会获得最大的收益:
// note: fields -> generally non-public
Type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
Run Code Online (Sandbox Code Playgroud)
而对于物业而言则相反:您可能会获得最大的收益:
// note: properties -> generally public
Type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
Run Code Online (Sandbox Code Playgroud)
显然,如果要查找特定类型(字段/属性)的所有(公共和非公共)成员,您将不得不使用:
Type.GetFields( // (or GetProperties)
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
)
Run Code Online (Sandbox Code Playgroud)