目标:以编程方式确定类字段的大小(以字节为单位)。例如,请参阅下面的评论...
class MyClass
{
public byte b ;
public short s ;
public int i ;
}
class MainClass
{
public static void Main()
{
foreach ( FieldInfo fieldInfo
in typeof(MyClass).GetFields(BindingFlags.Instance
| BindingFlags.Public | BindingFlags.NonPublic) )
Console.WriteLine ( fieldInfo.FieldType ) ;
// output is:
// System.Byte
// System.Int16
// System.Int32
// desired: to include "sizeof" each type (in bytes) ...
// System.Byte 1
// System.Int16 2
// System.Int32 4
}
}