zoukankan      html  css  js  c++  java
  • .NET:CLR via C# A Brief Look at Metadata

    基础知识

    A managed PE file has four main parts: the PE32(+) header, the CLR header, the metadata, and the IL. The PE32(+) header is the standard information that Windows expects. The CLR header is a small block of information that is specific to modules that require the CLR (managed modules). The header includes the major and minor version number of the CLR that the module was built for: some flags, a MethodDef token (described later) indicating the module’s entry point method if this module is a CUI, GUI, or Windows Store executable, and an optional strong-name digital signature. Finally, the header contains the size and offsets of certain metadata tables contained within the module.

    The metadata is a block of binary data that consists of several tables. There are three categories of tables: definition tables, reference tables, and manifest tables. T

    Metadata table entries are also created as the compiler detects the types, fields, methods, properties, and events that the source code references. The metadata created includes a set of reference tables that keep a record of the referenced items.

    示例

    C# 代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Main
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine(Type.GetType("A.ClassA, A"));
    14         }
    15     }
    16 
    17     public class ClassMain
    18     {
    19         public string Property { get; set; }
    20 
    21         public void Method(string msg)
    22         {
    23             Console.WriteLine(msg);
    24         }
    25     }
    26 }

    元数据

      1 ===========================================================
      2 ScopeName : Main.exe
      3 MVID      : {8C836651-F32E-4341-B87A-3479E4412916}
      4 ===========================================================
      5 Global functions
      6 -------------------------------------------------------
      7 
      8 Global fields
      9 -------------------------------------------------------
     10 
     11 Global MemberRefs
     12 -------------------------------------------------------
     13 
     14 TypeDef #1 (02000002)
     15 -------------------------------------------------------
     16     TypDefName: Main.Program  (02000002)
     17     Flags     : [NotPublic] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit]  (00100000)
     18     Extends   : 01000001 [TypeRef] System.Object
     19     Method #1 (06000001) [ENTRYPOINT]
     20     -------------------------------------------------------
     21         MethodName: Main (06000001)
     22         Flags     : [Private] [Static] [HideBySig] [ReuseSlot]  (00000091)
     23         RVA       : 0x00002050
     24         ImplFlags : [IL] [Managed]  (00000000)
     25         CallCnvntn: [DEFAULT]
     26         ReturnType: Void
     27         1 Arguments
     28             Argument #1:  SZArray String
     29         1 Parameters
     30             (1) ParamToken : (08000001) Name : args flags: [none] (00000000)
     31 
     32     Method #2 (06000002) 
     33     -------------------------------------------------------
     34         MethodName: .ctor (06000002)
     35         Flags     : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor]  (00001886)
     36         RVA       : 0x00002063
     37         ImplFlags : [IL] [Managed]  (00000000)
     38         CallCnvntn: [DEFAULT]
     39         hasThis 
     40         ReturnType: Void
     41         No arguments.
     42 
     43 
     44 TypeDef #2 (02000003)
     45 -------------------------------------------------------
     46     TypDefName: Main.ClassMain  (02000003)
     47     Flags     : [Public] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit]  (00100001)
     48     Extends   : 01000001 [TypeRef] System.Object
     49     Field #1 (04000001)
     50     -------------------------------------------------------
     51         Field Name: <Property>k__BackingField (04000001)
     52         Flags     : [Private]  (00000001)
     53         CallCnvntn: [FIELD]
     54         Field type:  String
     55         CustomAttribute #1 (0c000001)
     56         -------------------------------------------------------
     57             CustomAttribute Type: 0a000008
     58             CustomAttributeName: System.Runtime.CompilerServices.CompilerGeneratedAttribute :: instance void .ctor()
     59             Length: 4
     60             Value : 01 00 00 00                                      >                <
     61             ctor args: ()
     62 
     63 
     64     Method #1 (06000003) 
     65     -------------------------------------------------------
     66         MethodName: get_Property (06000003)
     67         Flags     : [Public] [HideBySig] [ReuseSlot] [SpecialName]  (00000886)
     68         RVA       : 0x0000206c
     69         ImplFlags : [IL] [Managed]  (00000000)
     70         CallCnvntn: [DEFAULT]
     71         hasThis 
     72         ReturnType: String
     73         No arguments.
     74         CustomAttribute #1 (0c000006)
     75         -------------------------------------------------------
     76             CustomAttribute Type: 0a000008
     77             CustomAttributeName: System.Runtime.CompilerServices.CompilerGeneratedAttribute :: instance void .ctor()
     78             Length: 4
     79             Value : 01 00 00 00                                      >                <
     80             ctor args: ()
     81 
     82 
     83     Method #2 (06000004) 
     84     -------------------------------------------------------
     85         MethodName: set_Property (06000004)
     86         Flags     : [Public] [HideBySig] [ReuseSlot] [SpecialName]  (00000886)
     87         RVA       : 0x00002083
     88         ImplFlags : [IL] [Managed]  (00000000)
     89         CallCnvntn: [DEFAULT]
     90         hasThis 
     91         ReturnType: Void
     92         1 Arguments
     93             Argument #1:  String
     94         1 Parameters
     95             (1) ParamToken : (08000002) Name : value flags: [none] (00000000)
     96         CustomAttribute #1 (0c000007)
     97         -------------------------------------------------------
     98             CustomAttribute Type: 0a000008
     99             CustomAttributeName: System.Runtime.CompilerServices.CompilerGeneratedAttribute :: instance void .ctor()
    100             Length: 4
    101             Value : 01 00 00 00                                      >                <
    102             ctor args: ()
    103 
    104 
    105     Method #3 (06000005) 
    106     -------------------------------------------------------
    107         MethodName: Method (06000005)
    108         Flags     : [Public] [HideBySig] [ReuseSlot]  (00000086)
    109         RVA       : 0x0000208c
    110         ImplFlags : [IL] [Managed]  (00000000)
    111         CallCnvntn: [DEFAULT]
    112         hasThis 
    113         ReturnType: Void
    114         1 Arguments
    115             Argument #1:  String
    116         1 Parameters
    117             (1) ParamToken : (08000003) Name : msg flags: [none] (00000000)
    118 
    119     Method #4 (06000006) 
    120     -------------------------------------------------------
    121         MethodName: .ctor (06000006)
    122         Flags     : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor]  (00001886)
    123         RVA       : 0x00002096
    124         ImplFlags : [IL] [Managed]  (00000000)
    125         CallCnvntn: [DEFAULT]
    126         hasThis 
    127         ReturnType: Void
    128         No arguments.
    129 
    130     Property #1 (17000001)
    131     -------------------------------------------------------
    132         Prop.Name : Property (17000001)
    133         Flags     : [none] (00000000)
    134         CallCnvntn: [PROPERTY]
    135         hasThis 
    136         ReturnType: String
    137         No arguments.
    138         DefltValue: 
    139         Setter    : (06000004) set_Property
    140         Getter    : (06000003) get_Property
    141         0 Others
    142 
    143 
    144 TypeRef #1 (01000001)
    145 -------------------------------------------------------
    146 Token:             0x01000001
    147 ResolutionScope:   0x23000001
    148 TypeRefName:       System.Object
    149     MemberRef #1 (0a000007)
    150     -------------------------------------------------------
    151         Member: (0a000007) .ctor: 
    152         CallCnvntn: [DEFAULT]
    153         hasThis 
    154         ReturnType: Void
    155         No arguments.
    156 
    157 TypeRef #2 (01000002)
    158 -------------------------------------------------------
    159 Token:             0x01000002
    160 ResolutionScope:   0x23000001
    161 TypeRefName:       System.Runtime.Versioning.TargetFrameworkAttribute
    162     MemberRef #1 (0a000001)
    163     -------------------------------------------------------
    164         Member: (0a000001) .ctor: 
    165         CallCnvntn: [DEFAULT]
    166         hasThis 
    167         ReturnType: Void
    168         1 Arguments
    169             Argument #1:  String
    170 
    171 TypeRef #3 (01000003)
    172 -------------------------------------------------------
    173 Token:             0x01000003
    174 ResolutionScope:   0x23000001
    175 TypeRefName:       System.Diagnostics.DebuggableAttribute
    176     MemberRef #1 (0a000002)
    177     -------------------------------------------------------
    178         Member: (0a000002) .ctor: 
    179         CallCnvntn: [DEFAULT]
    180         hasThis 
    181         ReturnType: Void
    182         1 Arguments
    183             Argument #1:  ValueClass DebuggingModes
    184 
    185 TypeRef #4 (01000004)
    186 -------------------------------------------------------
    187 Token:             0x01000004
    188 ResolutionScope:   0x01000003
    189 TypeRefName:       DebuggingModes
    190 
    191 TypeRef #5 (01000005)
    192 -------------------------------------------------------
    193 Token:             0x01000005
    194 ResolutionScope:   0x23000001
    195 TypeRefName:       System.Runtime.CompilerServices.CompilationRelaxationsAttribute
    196     MemberRef #1 (0a000003)
    197     -------------------------------------------------------
    198         Member: (0a000003) .ctor: 
    199         CallCnvntn: [DEFAULT]
    200         hasThis 
    201         ReturnType: Void
    202         1 Arguments
    203             Argument #1:  I4
    204 
    205 TypeRef #6 (01000006)
    206 -------------------------------------------------------
    207 Token:             0x01000006
    208 ResolutionScope:   0x23000001
    209 TypeRefName:       System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
    210     MemberRef #1 (0a000004)
    211     -------------------------------------------------------
    212         Member: (0a000004) .ctor: 
    213         CallCnvntn: [DEFAULT]
    214         hasThis 
    215         ReturnType: Void
    216         No arguments.
    217 
    218 TypeRef #7 (01000007)
    219 -------------------------------------------------------
    220 Token:             0x01000007
    221 ResolutionScope:   0x23000001
    222 TypeRefName:       System.Type
    223     MemberRef #1 (0a000005)
    224     -------------------------------------------------------
    225         Member: (0a000005) GetType: 
    226         CallCnvntn: [DEFAULT]
    227         ReturnType: Class System.Type
    228         1 Arguments
    229             Argument #1:  String
    230 
    231 TypeRef #8 (01000008)
    232 -------------------------------------------------------
    233 Token:             0x01000008
    234 ResolutionScope:   0x23000001
    235 TypeRefName:       System.Console
    236     MemberRef #1 (0a000006)
    237     -------------------------------------------------------
    238         Member: (0a000006) WriteLine: 
    239         CallCnvntn: [DEFAULT]
    240         ReturnType: Void
    241         1 Arguments
    242             Argument #1:  Object
    243     MemberRef #2 (0a000009)
    244     -------------------------------------------------------
    245         Member: (0a000009) WriteLine: 
    246         CallCnvntn: [DEFAULT]
    247         ReturnType: Void
    248         1 Arguments
    249             Argument #1:  String
    250 
    251 TypeRef #9 (01000009)
    252 -------------------------------------------------------
    253 Token:             0x01000009
    254 ResolutionScope:   0x23000001
    255 TypeRefName:       System.Runtime.CompilerServices.CompilerGeneratedAttribute
    256     MemberRef #1 (0a000008)
    257     -------------------------------------------------------
    258         Member: (0a000008) .ctor: 
    259         CallCnvntn: [DEFAULT]
    260         hasThis 
    261         ReturnType: Void
    262         No arguments.
    263 
    264 Signature #1 (0x11000001)
    265 -------------------------------------------------------
    266         CallCnvntn: [LOCALSIG]
    267         1 Arguments
    268             Argument #1:  String
    269 
    270 Assembly
    271 -------------------------------------------------------
    272     Token: 0x20000001
    273     Name : Main
    274     Public Key    :
    275     Hash Algorithm : 0x00008004
    276     Version: 0.0.0.0
    277     Major Version: 0x00000000
    278     Minor Version: 0x00000000
    279     Build Number: 0x00000000
    280     Revision Number: 0x00000000
    281     Locale: <null>
    282     Flags : [none] (00000000)
    283     CustomAttribute #1 (0c000002)
    284     -------------------------------------------------------
    285         CustomAttribute Type: 0a000001
    286         CustomAttributeName: System.Runtime.Versioning.TargetFrameworkAttribute :: instance void .ctor(class System.String)
    287         Length: 73
    288         Value : 01 00 1a 2e 4e 45 54 46  72 61 6d 65 77 6f 72 6b >   .NETFramework<
    289                       : 2c 56 65 72 73 69 6f 6e  3d 76 34 2e 35 01 00 54 >,Version=v4.5  T<
    290                       : 0e 14 46 72 61 6d 65 77  6f 72 6b 44 69 73 70 6c >  FrameworkDispl<
    291                       : 61 79 4e 61 6d 65 12 2e  4e 45 54 20 46 72 61 6d >ayName .NET Fram<
    292                       : 65 77 6f 72 6b 20 34 2e  35                      >ework 4.5       <
    293         ctor args: (".NETFramework,Version=v4.5")
    294 
    295     CustomAttribute #2 (0c000003)
    296     -------------------------------------------------------
    297         CustomAttribute Type: 0a000002
    298         CustomAttributeName: System.Diagnostics.DebuggableAttribute :: instance void .ctor(value class DebuggingModes)
    299         Length: 8
    300         Value : 01 00 07 01 00 00 00 00                          >                <
    301         ctor args: ( <can not decode> )
    302 
    303     CustomAttribute #3 (0c000004)
    304     -------------------------------------------------------
    305         CustomAttribute Type: 0a000003
    306         CustomAttributeName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute :: instance void .ctor(int32)
    307         Length: 8
    308         Value : 01 00 08 00 00 00 00 00                          >                <
    309         ctor args: (8)
    310 
    311     CustomAttribute #4 (0c000005)
    312     -------------------------------------------------------
    313         CustomAttribute Type: 0a000004
    314         CustomAttributeName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute :: instance void .ctor()
    315         Length: 30
    316         Value : 01 00 01 00 54 02 16 57  72 61 70 4e 6f 6e 45 78 >    T  WrapNonEx<
    317                       : 63 65 70 74 69 6f 6e 54  68 72 6f 77 73 01       >ceptionThrows   <
    318         ctor args: ()
    319 
    320 
    321 AssemblyRef #1 (23000001)
    322 -------------------------------------------------------
    323     Token: 0x23000001
    324     Public Key or Token: b7 7a 5c 56 19 34 e0 89 
    325     Name: mscorlib
    326     Version: 4.0.0.0
    327     Major Version: 0x00000004
    328     Minor Version: 0x00000000
    329     Build Number: 0x00000000
    330     Revision Number: 0x00000000
    331     Locale: <null>
    332     HashValue Blob:
    333     Flags: [none] (00000000)
    334 
    335 
    336 User Strings
    337 -------------------------------------------------------
    338 70000001 : (11) L"A.ClassA, A"
    339 
    340 
    341 Coff symbol name overhead:  0
    342 ===========================================================
    343 ===========================================================
    344 ===========================================================
  • 相关阅读:
    MySQL中如何使用布尔类型【转】
    你所不知道的Android Studio调试技巧【转】
    设计模式之工厂模式(factory pattern)【转】
    layuiadmin+tp5后台内容管理系统【转】
    PHPStorm怎么修改选中的背景颜色呢?【转】
    PHP保留两位小数的几种方法【转】
    jquery的css()函数同时设置多个css属性值
    Flutter text设置行间距【转】
    Flutter入门-布局Container、Padding、Align、Center【转】
    redis下载地址
  • 原文地址:https://www.cnblogs.com/happyframework/p/3396955.html
Copyright © 2011-2022 走看看