如何迭代 C# 类 (.NET 2.0) 的属性?

发布于 2024-08-20 07:50:45 字数 335 浏览 7 评论 0原文

假设我有一个类:

public class TestClass
{
  public String Str1;
  public String Str2;
  private String Str3;

  public String Str4 { get { return Str3; } }

  public TestClass()
  {
    Str1 = Str2 = Str 3 = "Test String";
  }
}

有没有一种方法(C# .NET 2)可以迭代“TestClass”类并打印出公共变量和属性?

记住.Net2

谢谢

Say I have a class:

public class TestClass
{
  public String Str1;
  public String Str2;
  private String Str3;

  public String Str4 { get { return Str3; } }

  public TestClass()
  {
    Str1 = Str2 = Str 3 = "Test String";
  }
}

Is there a way (C# .NET 2) to iterate through the Class 'TestClass' and print out public variables and attributes?

Remeber .Net2

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初心未许 2024-08-27 07:50:45

迭代公共实例属性:

Type classType = typeof(TestClass);
foreach(PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
  Console.WriteLine(property.Name);
}

迭代公共实例字段:

Type classType = typeof(TestClass);
foreach(FieldInfo field in classType.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
  Console.WriteLine(field.Name);
}

如果您还想包含非公共属性,请将 BindingFlags.NonPublic 添加到 GetProperties 和 <代码>GetFields。

To iterate through public instance properties:

Type classType = typeof(TestClass);
foreach(PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
  Console.WriteLine(property.Name);
}

To iterate through public instance fields:

Type classType = typeof(TestClass);
foreach(FieldInfo field in classType.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
  Console.WriteLine(field.Name);
}

If you also want to include non-public properties, add BindingFlags.NonPublic to the arguments of GetProperties and GetFields.

廻憶裏菂餘溫 2024-08-27 07:50:45

您可以使用反射来执行此操作。

这是一篇文章,它使用反射来实现可扩展性。

You can do this using reflection.

Here is an article that uses reflection for extensibility.

坐在坟头思考人生 2024-08-27 07:50:45

您可以使用反射

TestClass sample = new TestClass();
BindingFlags flags = BindingFlags.Instance | 
    BindingFlags.Public | BindingFlags.NonPublic;

foreach (FieldInfo f in sample.GetType().GetFields(flags))
    Console.WriteLine("{0} = {1}", f.Name, f.GetValue(sample));

foreach (PropertyInfo p in sample.GetType().GetProperties(flags))
    Console.WriteLine("{0} = {1}", p.Name, p.GetValue(sample, null));

You can to use reflection

TestClass sample = new TestClass();
BindingFlags flags = BindingFlags.Instance | 
    BindingFlags.Public | BindingFlags.NonPublic;

foreach (FieldInfo f in sample.GetType().GetFields(flags))
    Console.WriteLine("{0} = {1}", f.Name, f.GetValue(sample));

foreach (PropertyInfo p in sample.GetType().GetProperties(flags))
    Console.WriteLine("{0} = {1}", p.Name, p.GetValue(sample, null));
红衣飘飘貌似仙 2024-08-27 07:50:45

要获取类型的属性,我们将使用:

Type classType = typeof(TestClass);
    PropertyInfo[] properties = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

要获取类定义的属性,我们将使用:

Type classType = typeof(TestClass);
object[] attributes = classType.GetCustomAttributes(false); 

传递的布尔标志是继承标志,是否在继承链中搜索。

要获取属性的属性,我们将使用:

propertyInfo.GetCustomAttributes(false); 

使用上面给出的哈佛代码:

Type classType = typeof(TestClass);
object[] classAttributes = classType.GetCustomAttributes(false); 
foreach(PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
  object[] propertyAttributes = property.GetCustomAttributes(false); 
  Console.WriteLine(property.Name);
}

To get Properties of a Type we will use:

Type classType = typeof(TestClass);
    PropertyInfo[] properties = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

To get Attributes defined of a Class we will use:

Type classType = typeof(TestClass);
object[] attributes = classType.GetCustomAttributes(false); 

Boolean flag passed is the Inheritance flag, whether to search in inheritance chain or not.

To get attributes of a property we will use:

propertyInfo.GetCustomAttributes(false); 

Using Harvard Code given above:

Type classType = typeof(TestClass);
object[] classAttributes = classType.GetCustomAttributes(false); 
foreach(PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
  object[] propertyAttributes = property.GetCustomAttributes(false); 
  Console.WriteLine(property.Name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文