C#中的反射 - 想要一个类'字段的数据类型列表

时间:2009-07-09 14:04:36

标签: c# reflection types field

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

我想使用反射来获取这些成员的数据类型列表; 例如(借用Python表示法):[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

我希望程序输出显示为:

<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

4 个答案:

答案 0 :(得分:10)

这就是我做的,你想要实际返回Type实例的FieldType。

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

答案 1 :(得分:1)

我不确定MemberInfo是否包含您想要的信息。您可能希望查看GetFields()FieldInfo类,或GetProperties()PropertyInfo类。

GetMembers()会返回所有字段,属性和方法,因此如果您的类包含这些字段,那么它们也会被枚举。

答案 2 :(得分:1)

您正在寻找FieldType的Name属性,该属性可通过FieldInfo获得。您需要首先将MemberInfo转换为FieldInfo:

foreach (MemberInfo mi in theMemberInfoArray)
{
    if (mi.MemberType == MemberTypes.Field)
    {
        FieldInfo fi = mi as FieldInfo;
        Console.WriteLine(fi.FieldType.Name);
    }
}          

输出:

Char[]
UInt16
Char
Byte
UInt32
UInt64

答案 3 :(得分:0)

mi.Name带回来想要你想要的,你需要改变你的COnsole.WriteLine再次打印它