次のような 2 つの属性があります。
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class Test1Attribute : Attribute
{ }
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)]
sealed class Test2Attribute : Attribute
{ }
それらは非常に単純で、何もしません。
そして、これら 2 つの属性で装飾されたメソッドがあります。
public void Hello([Test1]string arg, [Test2] string arg2) { }
コードをコンパイルして IL Dasm で逆コンパイルすると、メソッド「Hello」の IL コードは次のようになります。
.method public hidebysig instance void Hello(int32 arg, int32 arg2) cil managed
{
.param [1]
.custom instance void ConsoleApplication1.Test1Attribute::.ctor()
.param [2]
.custom instance void ConsoleApplication1.Test2Attribute::.ctor()
.maxstack 8
L_0000: nop
L_0001: ret
}
Test1Attribute と Test2Attribute の両方が IL コードにあることがわかります。そのメタデータは次のようになります。
MethodName: Hello (06000005)
Flags : [Public] [HideBySig] [ReuseSlot] (00000086)
RVA : 0x0000206b
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
2 Arguments
Argument #1: String
Argument #2: String
2 Parameters
(1) ParamToken : (08000002) Name : arg flags: [none] (00000000)
CustomAttribute #1 (0c000010)
-------------------------------------------------------
CustomAttribute Type: 06000001
CustomAttributeName: ConsoleApplication1.Test1Attribute :: instance void .ctor()
Length: 4
Value : 01 00 00 00 > <
ctor args: ()
(2) ParamToken : (08000003) Name : arg2 flags: [none] (00000000)
CustomAttribute #1 (0c000012)
-------------------------------------------------------
CustomAttribute Type: 06000002
CustomAttributeName: ConsoleApplication1.Test2Attribute :: instance void .ctor()
Length: 4
Value : 01 00 00 00 > <
ctor args: ()
ここでも、両方の属性がメタデータに存在します。
だから私は興味があります:
- IL とメタデータの両方に表示されるのはなぜですか?
何が
.param [1] .custom インスタンス void ConsoleApplication1.Test1Attribute::.ctor() .param [2] .custom インスタンス void ConsoleApplication1.Test2Attribute::.ctor()
平均?指導のようには見えません。それで、彼らは何ですか?彼らは何をしますか?
ありがとう