Reflection in C# — want a list of the data types o

2019-05-20 22:29发布

问题:

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

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 ) ;
    }

}

Program output appears as below:

<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

回答1:

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;
}

This is the output:

<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


回答2:

I'm not sure that MemberInfo has the information you want. You might want to look at GetFields() and the FieldInfo class, or GetProperties() and the PropertyInfo class.

GetMembers() returns all fields, properties and methods, so if your class contained these they would be enumerated as well.



回答3:

You're looking for the Name property off of FieldType, which is available via FieldInfo. You'll need to cast MemberInfo to a FieldInfo first:

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

Output:

Char[]
UInt16
Char
Byte
UInt32
UInt64


回答4:

mi.Name is bringing back want you want, you jsut need to change your COnsole.WriteLine to print it again