【问题标题】:What does the IL ".custom instance void [attribute] = (...)" mean?IL“.custom instance void [attribute] = (...)”是什么意思?
【发布时间】:2019-01-18 11:54:09
【问题描述】:

当我对参数使用 params 关键字时,我在 IL 中找到了这一行。我从中了解到的是调用了 ParamArrayAttribute 类的构造函数,但我不明白 01 00 00 00 是什么意思。

.custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = (
            01 00 00 00
        )

【问题讨论】:

    标签: c# parameters cil


    【解决方案1】:

    请参考C# specification (version 6) defined in ECMA-335的第II.21节:

    Custom attributes are declared using the directive .custom, followed by the method
    declaration for a type constructor, optionally followed by a Bytes in parentheses:
    CustomDecl ::=
        Ctor [ ‘=’ ‘(’ Bytes ‘)’ ]
    

    Bytes 段的格式在第 II.23.3 节中定义:

    CustomAttrib starts with a Prolog – an unsigned int16, with value 0x0001.
    ...
    Next is a description of the optional “named” fields and properties. This starts with
    NumNamed – an unsigned int16 giving the number of “named” properties or fields that
    follow. Note that NumNamed shall always be present. A value of zero indicates that there
    are no “named” properties or fields to follow (and of course, in this case, the
    CustomAttrib shall end immediately after NumNamed).
    

    VI.B.3 节提供了各种自定义属性的示例。

    对于ParamArrayAttribute,前两个字节(01 00)是Prolog(小端格式),后两个字节(00 00)是NumNamed(0 = 否论据)。

    【讨论】:

      【解决方案2】:

      当用于装饰成员时,属性可能包含初始化参数,这些参数将被发送到属性的构造函数。

      举例

      [DataMember(EmitDefaultValue = true)]
      public int Property { get; set; }
      

      编译成

      .custom instance void
      [System.Runtime.Serialization]System.Runtime.Serialization.DataMemberAttribute::.ctor() = 
      ( 01 00 01 00 54 02 10 45 6D 69 74 44 65 66 61 75 6C 74 56 61 6C 75 65 01 )
      

      你看到的字节码序列对应EmitDefaultValue参数的初始化。

      在你的情况下,

      01 00 00 00
      

      是没有参数传递给属性构造器时使用的字节序列。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多