gpt4 book ai didi

c# - 为什么我的 Type.GetFields(BindingFlags.Instance|BindingFlags.Public) 不工作?

转载 作者:太空狗 更新时间:2023-10-29 23:58:34 27 4
gpt4 key购买 nike

我的代码可以看到非公共(public)成员,但看不到公共(public)成员。为什么?

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并且该类没有任何公共(public)字段:属性和字段是两个不同的东西。

尝试使用 GetProperties方法而不是 GetFields .

关于c# - 为什么我的 Type.GetFields(BindingFlags.Instance|BindingFlags.Public) 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3024218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com