C#中的反射-想要一个类的字段的数据类型的列表 [英] Reflection in C# -- want a list of the data types of a class' fields

查看:99
本文介绍了C#中的反射-想要一个类的字段的数据类型的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#类MyClass(如下)具有成员a,b,c,d,e和f.

My C# class MyClass (below) has members a, b, c, d, e, and f.

我想使用反射来获取那些成员的数据类型的列表; 例如(从Python表示法借用):[char [],ushort,char,byte,uint,ulong].

I'd like to use reflection to obtain a list of the data types of those members; for example (borrowing from Python notation): [ char[], ushort, char, byte, uint, ulong ].

class MyClass
{
    public  char [ ]    a ;
    public  ushort      b ;
    public  char        c ;
    public  byte        d ;
    public  uint        e ;
    public  ulong       f ;
}

class MainClass
{
public static void Main ( string [] args )
    {
        // get an array (or some kind of list) of MyClass' fields' data types ...
        // for example:  { char[], ushort, char, byte, uint, ulong }

        // I've tried the following, but can't get a column of just the data types, alone ...
        MemberInfo[] theMemberInfoArray = typeof(MyClass).GetMembers() ;
        foreach (MemberInfo mi in theMemberInfoArray)
            if (mi.MemberType == MemberTypes.Field)
                    Console.WriteLine ( "<" + mi.MemberType + ">\t"
                    + "<" + mi.GetType() + ">\t"
                    + "<" + mi.Name + ">\t" + mi ) ;
    }

}

程序输出如下:

<Field> <System.Reflection.RtFieldInfo> <a>     Char[]  a
<Field> <System.Reflection.RtFieldInfo> <b>     UInt16  b
<Field> <System.Reflection.RtFieldInfo> <c>     Char    c
<Field> <System.Reflection.RtFieldInfo> <d>     Byte    d
<Field> <System.Reflection.RtFieldInfo> <e>     UInt32  e
<Field> <System.Reflection.RtFieldInfo> <f>     UInt64  f

我希望程序输出显示为:

I would like program output to appear as:

<Field> <System.Reflection.RtFieldInfo> <a>     <Char[]>     Char[] a
<Field> <System.Reflection.RtFieldInfo> <b>     <UInt16>     UInt16 b
<Field> <System.Reflection.RtFieldInfo> <c>     <Char>       Char   c
<Field> <System.Reflection.RtFieldInfo> <d>     <Byte>       Byte   d
<Field> <System.Reflection.RtFieldInfo> <e>     <UInt32>     UInt32 e
<Field> <System.Reflection.RtFieldInfo> <f>     <UInt64>     UInt64 f

推荐答案

这是我的工作方式,您希望FieldType实际返回Type实例.

this is how I did it, you want the FieldType which actually returns a Type instance.

var members = typeof(TestMe).GetFields().Select(m => new 
                                         { 
                                             Name = m.Name, 
                                             MemType = m.MemberType, 
                                             RtField = m.GetType(), 
                                             Type = m.FieldType 
                                         });

foreach (var item in members)
    Console.WriteLine("<{0}> <{1}> <{2}> <{3}> {3} {2}", item.MemType, item.RtField, item.Name, item.Type, item.Type, item.Name);

public class TestMe
{
    public string A;
    public int B;
    public byte C;
    public decimal D;
}

这是输出:

<Field> <System.Reflection.RtFieldInfo> <A> <System.String> System.String A 
<Field> <System.Reflection.RtFieldInfo> <B> <System.Int32> System.Int32 B
<Field> <System.Reflection.RtFieldInfo> <C> <System.Byte> System.Byte C
<Field> <System.Reflection.RtFieldInfo> <D> <System.Decimal> System.Decimal D

这篇关于C#中的反射-想要一个类的字段的数据类型的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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