.NET,C#,反思:列出一个字段,本身具有字段的字段 [英] .NET, C#, Reflection: list the fields of a field that, itself, has fields

查看:184
本文介绍了.NET,C#,反思:列出一个字段,本身具有字段的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET和放大器; C#中,假设 ClassB的有一个字段的类型为 ClassA的的。 人们可以很方便的使用方法 GetFields 列出 ClassB的的领域。 不过,我想的列表中的 ClassB的字段的自己的领域的有田。 例如, ClassB的的字段 X 有田 B 取值。我想(编程)列出这些字段(所建议的我在code以下注释)。

 类ClassA的
    {
    公共BYTE B;
    公共短S;
    公众诠释我;
    }

类ClassB的
    {
    众长升;
    公共ClassA的X;
    }

类MainClass
    {
    公共静态无效的主要()
    {
    ClassA的myAObject =新ClassA的();
    ClassB的myBObject =新ClassB的();

    //我的目标是这样的:
    // ***使用myBObject只有***,打印其字段,字段
    //这些字段,*自身*,有田。
    //输出应该是这样的:
    // Int64的升
    // ClassA的x
    // BYTE B
    // Int16的小号
    //的Int32我

    }
    }
 

解决方案

使用了 FieldInfo.FieldType ,以反映在您的类中的字段的类型。例如,

  fieldInfo.FieldType.GetFields();
 

下面是如果你有 ClassZ ClassA的。它打破了,如果你有一个循环的对象图。

 使用系统;
使用的System.Reflection;

类ClassA的{
  公共BYTE B;
  公共短S;
  公众诠释我;
}

类ClassB的{
  众长升;
  公共ClassA的X;
}

类MainClass {

  公共静态无效的主要(){
    ClassB的myBObject =新ClassB的();
    WriteFields(myBObject.GetType(),0);
  }

  静态无效WriteFields(类型类型的Int32缩进){
    的foreach(字段信息字段信息在type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic可)){
      Console.WriteLine({0} {1} \ t {2},新的String(\ t,缩进),fieldInfo.FieldType.Name,fieldInfo.Name);
      如果(fieldInfo.FieldType.IsClass)
        WriteFields(fieldInfo.FieldType,缩进+ 1);
    }
  }

}
 

In .NET & C#, suppose ClassB has a field that is of type ClassA. One can easily use method GetFields to list ClassB's fields. However, I want to also list the fields of those ClassB fields that themselves have fields. For example, ClassB's field x has fields b, s, and i. I'd like to (programmatically) list those fields (as suggested by my comments in the code below).

class ClassA
    {
    public	byte	b ;
    public	short	s ;
    public	int	i ;
    }

class ClassB
    {
    public	long	l ;
    public	ClassA	x ;
    }

class MainClass
    {
    public static void Main ( )
    	{
    	ClassA myAObject = new ClassA () ;
    	ClassB myBObject = new ClassB () ;

    	//  My goal is this:
    	//    ***Using myBObject only***, print its fields, and the fields
    	//    of those fields that, *themselves*, have fields.
    	//  The output should look like this:
    	//    Int64   l
    	//    ClassA  x
    	//               Byte   b
    	//               Int16  s
    	//               Int32  i

    	}
    }

解决方案

Use the FieldInfo.FieldType to reflect over the type of the fields in your class. E.g.

fieldInfo.FieldType.GetFields();

Here is a complete sample based on your code that uses recursion in case you have ClassZ inside ClassA. It breaks if you have a cyclic object graph.

using System;
using System.Reflection;

class ClassA {
  public byte b;
  public short s; 
  public int i;
}

class ClassB {
  public long l;
  public ClassA x;
}

class MainClass {

  public static void Main() {
    ClassB myBObject = new ClassB();
    WriteFields(myBObject.GetType(), 0);
  }

  static void WriteFields(Type type, Int32 indent) {
    foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
      Console.WriteLine("{0}{1}\t{2}", new String('\t', indent), fieldInfo.FieldType.Name, fieldInfo.Name);
      if (fieldInfo.FieldType.IsClass)
        WriteFields(fieldInfo.FieldType, indent + 1);
    }
  }

}

这篇关于.NET,C#,反思:列出一个字段,本身具有字段的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆