问题
I am trying to understand the IL code and C# internals specifically nowadays, i wrote a simple c# hello world program whose code is :
using System;
class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
and here is IL generated for the constuctor of Program
class :
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctor
I am not able to understand what is the meaning and purpose of specialname and rtspecialname or what's the use of them ?
回答1:
Per the ECMA 355 standard:
rtspecialname indicates that the name of this item has special significance to the CLI. There are no currently defined special type names; this is for future use. Any item marked rtspecialname shall also be marked specialname.
specialname indicates that the name of this item can have special significance to tools other than the CLI. See, for example, Partition I .
These attributes are used either by tools or the runtime to determine if an attribute or method has a special use or significance - one example (What other neat tricks does the SpecialNameAttribute allow?) is to mark an operator to prevent collisions with the user namespace. Another one I see in the standard is for getters and setters of properties, and it appears that the Main
method you cited is another example.
Although I marked the original question as a duplicate, this question is slightly different in scope and the answer here might shed some more light. I hope anyway.
来源:https://stackoverflow.com/questions/34026958/purpose-and-meaning-of-specialname-and-rtspecialname-in-il