淺談.NET框架中反射的內部機制


  在.NET眾多的特性中,可以說反射式.NET框架最獨特的一個特性,這一點在其他框架中沒有對應的概念。反射在.NET中的地位也是非常重要的,正是存在元素據等信息,才構成了.NET系統運行的基礎。

  需要說明的是反射在實際開發中的運用,遠遠沒有.NET框架自身對反射的應用多,某種程度上,理解反射的原理和機制,比能夠使用反射更為重要,因為能夠幫助程序員更透徹的理解.NET的機制和原理。。

  在.NET框架中,反射提供了一種動態分析、創建和調用的機制。在.NET框架中,一個系統可以由多個程序集組成,一個程序集可以包含多個模塊,而一個模塊中會有很多類型,每個類型可以包含字段和方法,方法又可以包含輸入參數和輸出參數、特性等多種信息。在其他技術框架中,使用一個模塊往往需要在編碼時徹底地了解其對外的接口,而在.NET中,反射機制使得動態分析程序集並且使用其中的類型和方法稱為了可能。

  這里DebugLZQ再簡單介紹.NET程序集的元數據概念。元數據,就是描述數據的數據。在CLR中,元數據就是對一個模塊定義或引用的所有東西的描述系統。因為CLR編程系統是面向對象的,所以元素據描述的東西就是類型和他們的成員以及伴隨而來的特性、屬性和聯系。元數據正是反射機制能夠運作的基石,和COM只記錄公共接口不同,元素據存儲了類型內部的所有信息,這也就意味着,程序可以動態地得到整個程序集的內部結構(注意:這里說的是能夠得到,但並不意味着可以訪問。對程序集的訪問仍然受到訪問級別的控制)。

  為了更直接的體會元素據的描述,下面寫一段簡單的測試代碼,並且查看它生成程序集的元素據。代碼如下:

using System;
using System.Reflection;
namespace 簡單程序集
{
    /// <summary>
    /// 構建一個包含私有成員、特性、方法的簡單類型
    /// </summary>
    [Serializable ]
    class Program
    {
        private string _MyString;
        public Program(string mystring)
        {
            _MyString = mystring;
        }
        public override string ToString()
        {
            return _MyString;
        }

        private static void ShowMemberInfo()
        {
            var assems = AppDomain.CurrentDomain.GetAssemblies();

            foreach (Assembly ass in assems)
            {
                foreach (Type t in ass.GetTypes())
                {
                    foreach (MemberInfo mi in t.GetMembers())
                    {
                        Console.WriteLine("Name:{0}, Type:{1}", mi.Name, mi.MemberType.ToString());
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Sample Assembly by DebugLZQ.");
            ShowMemberInfo();
            Console.ReadKey();
        }
    }
}

  編譯該程序集后,默認生成一個.exe的程序集。利用vs自帶的ildasm工具來查看一下這個程序集的元素據(快捷鍵Ctrl + M)。如下:

View Code
===========================================================

MVID      : {9419D95D-928D-4142-8803-99745DCC631E}
===========================================================
Global functions
-------------------------------------------------------

Global fields
-------------------------------------------------------

Global MemberRefs
-------------------------------------------------------

TypeDef #1 (02000002)
-------------------------------------------------------

    Flags     : [NotPublic] [AutoLayout] [Class] [Serializable] [AnsiClass] [BeforeFieldInit]  (00102000)
    Extends   : 01000001 [TypeRef] System.Object
    Field #1 (04000001)
    -------------------------------------------------------
        Field Name: _MyString (04000001)
        Flags     : [Private]  (00000001)
        CallCnvntn: [FIELD]
        Field type:  String

    Method #1 (06000001) 
    -------------------------------------------------------
        MethodName: .ctor (06000001)
        Flags     : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor]  (00001886)
        RVA       : 0x00002050
        ImplFlags : [IL] [Managed]  (00000000)
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String
        1 Parameters
            (1) ParamToken : (08000001) Name : mystring flags: [none] (00000000)

    Method #2 (06000002) 
    -------------------------------------------------------
        MethodName: ToString (06000002)
        Flags     : [Public] [Virtual] [HideBySig] [ReuseSlot]  (000000c6)
        RVA       : 0x00002064
        ImplFlags : [IL] [Managed]  (00000000)
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: String
        No arguments.

    Method #3 (06000003) 
    -------------------------------------------------------
        MethodName: ShowMemberInfo (06000003)
        Flags     : [Private] [Static] [HideBySig] [ReuseSlot]  (00000091)
        RVA       : 0x0000207c
        ImplFlags : [IL] [Managed]  (00000000)
        CallCnvntn: [DEFAULT]
        ReturnType: Void
        No arguments.

    Method #4 (06000004) [ENTRYPOINT]
    -------------------------------------------------------
        MethodName: Main (06000004)
        Flags     : [Private] [Static] [HideBySig] [ReuseSlot]  (00000091)
        RVA       : 0x00002135
        ImplFlags : [IL] [Managed]  (00000000)
        CallCnvntn: [DEFAULT]
        ReturnType: Void
        1 Arguments
            Argument #1:  SZArray String
        1 Parameters
            (1) ParamToken : (08000002) Name : args flags: [none] (00000000)


TypeRef #1 (01000001)
-------------------------------------------------------
Token:             0x01000001
ResolutionScope:   0x23000001
TypeRefName:       System.Object
    MemberRef #1 (0a000012)
    -------------------------------------------------------
        Member: (0a000012) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        No arguments.
    MemberRef #2 (0a000019)
    -------------------------------------------------------
        Member: (0a000019) ToString: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: String
        No arguments.

TypeRef #2 (01000002)
-------------------------------------------------------
Token:             0x01000002
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyTitleAttribute
    MemberRef #1 (0a000001)
    -------------------------------------------------------
        Member: (0a000001) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #3 (01000003)
-------------------------------------------------------
Token:             0x01000003
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyDescriptionAttribute
    MemberRef #1 (0a000002)
    -------------------------------------------------------
        Member: (0a000002) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #4 (01000004)
-------------------------------------------------------
Token:             0x01000004
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyConfigurationAttribute
    MemberRef #1 (0a000003)
    -------------------------------------------------------
        Member: (0a000003) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #5 (01000005)
-------------------------------------------------------
Token:             0x01000005
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyCompanyAttribute
    MemberRef #1 (0a000004)
    -------------------------------------------------------
        Member: (0a000004) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #6 (01000006)
-------------------------------------------------------
Token:             0x01000006
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyProductAttribute
    MemberRef #1 (0a000005)
    -------------------------------------------------------
        Member: (0a000005) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #7 (01000007)
-------------------------------------------------------
Token:             0x01000007
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyCopyrightAttribute
    MemberRef #1 (0a000006)
    -------------------------------------------------------
        Member: (0a000006) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #8 (01000008)
-------------------------------------------------------
Token:             0x01000008
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyTrademarkAttribute
    MemberRef #1 (0a000007)
    -------------------------------------------------------
        Member: (0a000007) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #9 (01000009)
-------------------------------------------------------
Token:             0x01000009
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyCultureAttribute
    MemberRef #1 (0a000008)
    -------------------------------------------------------
        Member: (0a000008) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #10 (0100000a)
-------------------------------------------------------
Token:             0x0100000a
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.InteropServices.ComVisibleAttribute
    MemberRef #1 (0a000009)
    -------------------------------------------------------
        Member: (0a000009) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  Boolean

TypeRef #11 (0100000b)
-------------------------------------------------------
Token:             0x0100000b
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.InteropServices.GuidAttribute
    MemberRef #1 (0a00000a)
    -------------------------------------------------------
        Member: (0a00000a) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #12 (0100000c)
-------------------------------------------------------
Token:             0x0100000c
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyVersionAttribute
    MemberRef #1 (0a00000b)
    -------------------------------------------------------
        Member: (0a00000b) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #13 (0100000d)
-------------------------------------------------------
Token:             0x0100000d
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.AssemblyFileVersionAttribute
    MemberRef #1 (0a00000c)
    -------------------------------------------------------
        Member: (0a00000c) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #14 (0100000e)
-------------------------------------------------------
Token:             0x0100000e
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.Versioning.TargetFrameworkAttribute
    MemberRef #1 (0a00000d)
    -------------------------------------------------------
        Member: (0a00000d) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  String

TypeRef #15 (0100000f)
-------------------------------------------------------
Token:             0x0100000f
ResolutionScope:   0x23000001
TypeRefName:       System.Diagnostics.DebuggableAttribute
    MemberRef #1 (0a00000e)
    -------------------------------------------------------
        Member: (0a00000e) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  ValueClass DebuggingModes

TypeRef #16 (01000010)
-------------------------------------------------------
Token:             0x01000010
ResolutionScope:   0x0100000f
TypeRefName:       DebuggingModes

TypeRef #17 (01000011)
-------------------------------------------------------
Token:             0x01000011
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.CompilerServices.CompilationRelaxationsAttribute
    MemberRef #1 (0a00000f)
    -------------------------------------------------------
        Member: (0a00000f) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  I4

TypeRef #18 (01000012)
-------------------------------------------------------
Token:             0x01000012
ResolutionScope:   0x23000001
TypeRefName:       System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
    MemberRef #1 (0a000010)
    -------------------------------------------------------
        Member: (0a000010) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        No arguments.

TypeRef #19 (01000013)
-------------------------------------------------------
Token:             0x01000013
ResolutionScope:   0x23000001
TypeRefName:       System.SerializableAttribute
    MemberRef #1 (0a000011)
    -------------------------------------------------------
        Member: (0a000011) .ctor: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        No arguments.

TypeRef #20 (01000014)
-------------------------------------------------------
Token:             0x01000014
ResolutionScope:   0x23000001
TypeRefName:       System.AppDomain
    MemberRef #1 (0a000013)
    -------------------------------------------------------
        Member: (0a000013) get_CurrentDomain: 
        CallCnvntn: [DEFAULT]
        ReturnType: Class System.AppDomain
        No arguments.
    MemberRef #2 (0a000014)
    -------------------------------------------------------
        Member: (0a000014) GetAssemblies: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: SZArray Class System.Reflection.Assembly
        No arguments.

TypeRef #21 (01000015)
-------------------------------------------------------
Token:             0x01000015
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.Assembly
    MemberRef #1 (0a000015)
    -------------------------------------------------------
        Member: (0a000015) GetTypes: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: SZArray Class System.Type
        No arguments.

TypeRef #22 (01000016)
-------------------------------------------------------
Token:             0x01000016
ResolutionScope:   0x23000001
TypeRefName:       System.Type
    MemberRef #1 (0a000016)
    -------------------------------------------------------
        Member: (0a000016) GetMembers: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: SZArray Class System.Reflection.MemberInfo
        No arguments.

TypeRef #23 (01000017)
-------------------------------------------------------
Token:             0x01000017
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.MemberInfo
    MemberRef #1 (0a000017)
    -------------------------------------------------------
        Member: (0a000017) get_Name: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: String
        No arguments.
    MemberRef #2 (0a000018)
    -------------------------------------------------------
        Member: (0a000018) get_MemberType: 
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: ValueClass System.Reflection.MemberTypes
        No arguments.

TypeRef #24 (01000018)
-------------------------------------------------------
Token:             0x01000018
ResolutionScope:   0x23000001
TypeRefName:       System.Reflection.MemberTypes

TypeRef #25 (01000019)
-------------------------------------------------------
Token:             0x01000019
ResolutionScope:   0x23000001
TypeRefName:       System.Console
    MemberRef #1 (0a00001a)
    -------------------------------------------------------
        Member: (0a00001a) WriteLine: 
        CallCnvntn: [DEFAULT]
        ReturnType: Void
        3 Arguments
            Argument #1:  String
            Argument #2:  Object
            Argument #3:  Object
    MemberRef #2 (0a00001b)
    -------------------------------------------------------
        Member: (0a00001b) WriteLine: 
        CallCnvntn: [DEFAULT]
        ReturnType: Void
        1 Arguments
            Argument #1:  String
    MemberRef #3 (0a00001c)
    -------------------------------------------------------
        Member: (0a00001c) ReadKey: 
        CallCnvntn: [DEFAULT]
        ReturnType: ValueClass System.ConsoleKeyInfo
        No arguments.

TypeRef #26 (0100001a)
-------------------------------------------------------
Token:             0x0100001a
ResolutionScope:   0x23000001
TypeRefName:       System.ConsoleKeyInfo

Signature #1 (0x11000001)
-------------------------------------------------------
        CallCnvntn: [LOCALSIG]
        1 Arguments
            Argument #1:  String

Signature #2 (0x11000002)
-------------------------------------------------------
        CallCnvntn: [LOCALSIG]
        11 Arguments
            Argument #1:  SZArray Class System.Reflection.Assembly
            Argument #2:  Class System.Reflection.Assembly
            Argument #3:  Class System.Type
            Argument #4:  Class System.Reflection.MemberInfo
            Argument #5:  SZArray Class System.Reflection.Assembly
            Argument #6:  I4
            Argument #7:  SZArray Class System.Type
            Argument #8:  I4
            Argument #9:  SZArray Class System.Reflection.MemberInfo
            Argument #10:  I4
            Argument #11:  Boolean

Assembly
-------------------------------------------------------
    Token: 0x20000001

    Public Key    :
    Hash Algorithm : 0x00008004
    Version: 1.0.0.0
    Major Version: 0x00000001
    Minor Version: 0x00000000
    Build Number: 0x00000000
    Revision Number: 0x00000000
    Locale: <null>
    Flags : [none] (00000000)
    CustomAttribute #1 (0c000001)
    -------------------------------------------------------
        CustomAttribute Type: 0a000001
        CustomAttributeName: System.Reflection.AssemblyTitleAttribute :: instance void .ctor(class System.String)
        Length: 20
        Value : 01 00 0f e7 ae 80 e5 8d  95 e7 a8 8b e5 ba 8f e9 >                <
                      : 9b 86 00 00                                      >                <
        ctor args: ("簡單程序集")

    CustomAttribute #2 (0c000002)
    -------------------------------------------------------
        CustomAttribute Type: 0a000002
        CustomAttributeName: System.Reflection.AssemblyDescriptionAttribute :: instance void .ctor(class System.String)
        Length: 5
        Value : 01 00 00 00 00                                   >                <
        ctor args: ("")

    CustomAttribute #3 (0c000003)
    -------------------------------------------------------
        CustomAttribute Type: 0a000003
        CustomAttributeName: System.Reflection.AssemblyConfigurationAttribute :: instance void .ctor(class System.String)
        Length: 5
        Value : 01 00 00 00 00                                   >                <
        ctor args: ("")

    CustomAttribute #4 (0c000004)
    -------------------------------------------------------
        CustomAttribute Type: 0a000004
        CustomAttributeName: System.Reflection.AssemblyCompanyAttribute :: instance void .ctor(class System.String)
        Length: 5
        Value : 01 00 00 00 00                                   >                <
        ctor args: ("")

    CustomAttribute #5 (0c000005)
    -------------------------------------------------------
        CustomAttribute Type: 0a000005
        CustomAttributeName: System.Reflection.AssemblyProductAttribute :: instance void .ctor(class System.String)
        Length: 20
        Value : 01 00 0f e7 ae 80 e5 8d  95 e7 a8 8b e5 ba 8f e9 >                <
                      : 9b 86 00 00                                      >                <
        ctor args: ("簡單程序集")

    CustomAttribute #6 (0c000006)
    -------------------------------------------------------
        CustomAttribute Type: 0a000006
        CustomAttributeName: System.Reflection.AssemblyCopyrightAttribute :: instance void .ctor(class System.String)
        Length: 23
        Value : 01 00 12 43 6f 70 79 72  69 67 68 74 20 c2 a9 20 >   Copyright    <
                      : 20 32 30 31 32 00 00                             > 2012           <
        ctor args: ("Copyright ©  2012")

    CustomAttribute #7 (0c000007)
    -------------------------------------------------------
        CustomAttribute Type: 0a000007
        CustomAttributeName: System.Reflection.AssemblyTrademarkAttribute :: instance void .ctor(class System.String)
        Length: 5
        Value : 01 00 00 00 00                                   >                <
        ctor args: ("")

    CustomAttribute #8 (0c000008)
    -------------------------------------------------------
        CustomAttribute Type: 0a000009
        CustomAttributeName: System.Runtime.InteropServices.ComVisibleAttribute :: instance void .ctor(bool)
        Length: 5
        Value : 01 00 00 00 00                                   >                <
        ctor args: ( <can not decode> )

    CustomAttribute #9 (0c000009)
    -------------------------------------------------------
        CustomAttribute Type: 0a00000a
        CustomAttributeName: System.Runtime.InteropServices.GuidAttribute :: instance void .ctor(class System.String)
        Length: 41
        Value : 01 00 24 66 61 66 61 61  65 66 38 2d 33 31 32 34 >  $fafaaef8-3124<
                      : 2d 34 36 34 39 2d 62 34  34 64 2d 37 65 63 35 35 >-4649-b44d-7ec55<
                      : 61 39 35 32 39 35 63 00  00                      >a95295c         <
        ctor args: ("fafaaef8-3124-4649-b44d-7ec55a95295c")

    CustomAttribute #10 (0c00000a)
    -------------------------------------------------------
        CustomAttribute Type: 0a00000c
        CustomAttributeName: System.Reflection.AssemblyFileVersionAttribute :: instance void .ctor(class System.String)
        Length: 12
        Value : 01 00 07 31 2e 30 2e 30  2e 30 00 00             >   1.0.0.0      <
        ctor args: ("1.0.0.0")

    CustomAttribute #11 (0c00000b)
    -------------------------------------------------------
        CustomAttribute Type: 0a00000d
        CustomAttributeName: System.Runtime.Versioning.TargetFrameworkAttribute :: instance void .ctor(class System.String)
        Length: 101
        Value : 01 00 29 2e 4e 45 54 46  72 61 6d 65 77 6f 72 6b >  ).NETFramework<
                      : 2c 56 65 72 73 69 6f 6e  3d 76 34 2e 30 2c 50 72 >,Version=v4.0,Pr<
                      : 6f 66 69 6c 65 3d 43 6c  69 65 6e 74 01 00 54 0e >ofile=Client  T <
                      : 14 46 72 61 6d 65 77 6f  72 6b 44 69 73 70 6c 61 > FrameworkDispla<
                      : 79 4e 61 6d 65 1f 2e 4e  45 54 20 46 72 61 6d 65 >yName .NET Frame<
                      : 77 6f 72 6b 20 34 20 43  6c 69 65 6e 74 20 50 72 >work 4 Client Pr<
                      : 6f 66 69 6c 65                                   >ofile           <
        ctor args: (".NETFramework,Version=v4.0,Profile=Client")

    CustomAttribute #12 (0c00000c)
    -------------------------------------------------------
        CustomAttribute Type: 0a00000e
        CustomAttributeName: System.Diagnostics.DebuggableAttribute :: instance void .ctor(value class DebuggingModes)
        Length: 8
        Value : 01 00 07 01 00 00 00 00                          >                <
        ctor args: ( <can not decode> )

    CustomAttribute #13 (0c00000d)
    -------------------------------------------------------
        CustomAttribute Type: 0a00000f
        CustomAttributeName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute :: instance void .ctor(int32)
        Length: 8
        Value : 01 00 08 00 00 00 00 00                          >                <
        ctor args: (8)

    CustomAttribute #14 (0c00000e)
    -------------------------------------------------------
        CustomAttribute Type: 0a000010
        CustomAttributeName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute :: instance void .ctor()
        Length: 30
        Value : 01 00 01 00 54 02 16 57  72 61 70 4e 6f 6e 45 78 >    T  WrapNonEx<
                      : 63 65 70 74 69 6f 6e 54  68 72 6f 77 73 01       >ceptionThrows   <
        ctor args: ()


AssemblyRef #1 (23000001)
-------------------------------------------------------
    Token: 0x23000001
    Public Key or Token: b7 7a 5c 56 19 34 e0 89 
    Name: mscorlib
    Version: 4.0.0.0
    Major Version: 0x00000004
    Minor Version: 0x00000000
    Build Number: 0x00000000
    Revision Number: 0x00000000
    Locale: <null>
    HashValue Blob:
    Flags: [none] (00000000)


User Strings
-------------------------------------------------------
70000001 : (18) L"Name:{0}, Type:{1}"
70000027 : (28) L"Sample Assembly by DebugLZQ."


Coff symbol name overhead:  0
===========================================================
===========================================================
===========================================================

  縱觀整個元素據表,大致可以分為定義表、引用表、指針表、堆等:

  • 定義表,描述了源代碼中定義的類型和成員信息,主要包括:TypeDef、MehodDef、FieldDef、ModuleDef、PropertyDef等。
  • 引用表,描述了源代碼中引用的類型和成員信息,引用元素可以是同一程序集的其他模塊,也可以是不同程序集的模塊,主要包括:AssemblyRef、TypeRef、ModuleRef、MethodsRef等。
  • 指針表,使用指針表引用未知代碼,主要包括:MethodPtr、FieldPtr、ParamPtr等。
  • 堆,以stream的形式保存的信息堆,主要包括:#String、#Blob、#US、#GUIDe等。 

  如前文所述,我們以ILDasm.exe可以通過反編譯的方式,通過執行Ctrl+M快捷鍵來獲取該程序集所使用的MetaData信息列表,在.NET中每個模塊包含了44個CLR元數據表,如下:

表記錄 元數據表 說明
0(0) ModuleDef 描述當前模塊
1(0x1) TypeRef 描述引用Type,為每個引用到類型保存一條記錄
2(0x2) TypeDef 描述Type定義,每個Type將在TypeDef表中保存一條記錄
3(0x3) FieldPtr 描述字段指針,定義類的字段時的中間查找表
4(0x4) FieldDef 描述字段定義
5(0x5) MethodPtr 描述方法指針,定義類的方法時的中間查找表
6(0x6) MethodDef 描述方法定義
7(0x7) ParamPtr 描述參數指針,定義類的參數時的中間查找表
8(0x8) ParamDef 描述方法的參數定義
9(0x9) InterfaceImpl 描述有哪些類型實現了哪些接口
10(0xa) MemberRef 描述引用成員的情況,引用成員可以是方法、字段還有屬性。
11(0xb) Constant 描述了參數、字段和屬性的常數值
12(0xc) CustomAttribute 描述了特性的定義
13(0xd) FieldMarshal 描述了與非托管代碼交互時,參數和字段的傳遞方式。
14(0xe) DeclSecurity 描述了對於類、方法和程序集的安全性
15(0xf) ClassLayout 描述類加載時的布局信息
16(0x10) FieldLayout 描述單個字段的偏移或序號
17(0x11) StandAloneSig 描述未被任何其他表引用的簽名
18(0x12) EventMap 描述類的事件列表
19(0x13) EventPtr 描述了事件指針,定義事件時的中間查找表
20(0x14) Event 描述事件
21(0x15) PropertyMap 描述類的屬性列表
22(0x16) PropertyPtr 描述了屬性指針,定義類的屬性時的中間查找表
23(0x17) Property 描述屬性
24(0x18) MethodSemantics 描述事件、屬性與方法的關聯
25(0x19) MethodImpl 描述方法的實現
26(0x1a) ModuleRef 描述外部模塊的引用
27(0x1b) TypeSpec 描述了對TypeDef或者TypeRef的說明
28(0x1c) ImplMap 描述了程序集使用的所有非托管代碼的方法
29(0x1d) FieldRVA 字段表的擴展,RVA給出了一個字段的原始值位置
30(0x1e) ENCLog 描述在Edit-And-Continue模式中哪些元數據被修改過
31(0x1f) ENCMap 描述在Edit-And-Continue模式中的映射
32(0x20) Assembly 描述程序集定義
33(0x21) AssemblyProcessor 未使用
34(0x22) AssemblyOS 未使用
35(0x23) AssemblyRef 描述引用的程序集
36(0x24) AssemblyRefProcessor 未使用
37(0x25) AssemblyRefOS 未使用
38(0x26) File 描述外部文件
39(0x27) ExportedType 描述在同一程序集但不同模塊,有哪些類型
40(0x28) ManifestResource 描述資源信息
41(0x29) NestedClass 描述嵌套類型定義
42(0x2a) GenericParam 描述了泛型類型定義或者泛型方法定義所使用的泛型參數
43(0x2b) MethodSpec 描述泛型方法的實例化
44(0x2c) GenericParamConstraint 描述了每個泛型參數的約束

  然后是6個命名堆:

說明

#String 一個AscII string數組,被元數據表所引用,來表示方法名、字段名、類名、變量名以及資源相關字符串,但不包含string literals。
#Blob 包含元數據引用的二進制對象,但不包含用戶定義對象
#US 一個unicode string數組,包含了定義在代碼中的字符串(string literals),這些字符串可以直接由ldstr指令加載獲取,
#GUID 保存了128byte的GUID值,由元數據表引用
#~ 一個特殊堆,包含了所有的元數據表,會引用其他的堆。
#- 一個未壓縮的#~堆。除了#-堆,其他堆都是壓縮的。

   運行上面這段示例程序,能能夠獲得一個很長很長很長的運行時元素據信息。結果如下:

  正如以上分析的,程序集的內部結構在元數據中一覽無余,也可在運行時動態獲得元素據的相關信息,因為元素據的數據結構是標准固定的,這使得外部代碼可以輕松地動態獲得程序集的信息並使用。這也就實現了.NET中最獨特的特性:反射。

  DebugLZQ:反射的本質在於讀取元數據來分析模塊或者程序集,而並不規定讀取的方法。同時,.NET提供了多個類型方便程序員分析元數據,但反射機制概念和這些類型的使用並不綁定。歡迎批評指正!

 【請點擊下面的“綠色通道”---“關注DebugLZQ”,共同交流進步~】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM