IL中的以下代码行是什么意思?

时间:2019-01-18 11:54:10

标签: c# parameters cil

当我使用params关键字作为参数时,我在IL中找到了这一行。我从中了解到的是调用了ParamArrayAttribute类的构造函数,但我不理解01 00 00 00的含义。

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

2 个答案:

答案 0 :(得分:7)

请参阅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(采用Little-endian格式),后两个字节(00 00)是NumNamed(0 =无参数)。

答案 1 :(得分:4)

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

实例

$pages = DB::table('client_requests')
                ->leftJoin('bid_requests', 'client_requests.id', '=', 'bid_requests.client_request_id')
                ->select('client_requests.*')
                ->orderBy('created_at', 'DESC')
                ->paginate(20);

编译为

[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

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

相关问题