Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What other neat tricks does the SpecialNameAttribute allow?

In researching a question on implementing the Visual Basic Power (^) operator, I learned that the System.Runtime.CompilerServices.SpecialNameAttribute class allows one to implement this operator in C# for use in VB.

Interestingly, the documentation states:

The SpecialNameAttribute class is not currently used in the .NET Framework, but is reserved for future use.

My question is: What other neat tricks are available when using this attribute?

(side question - is the documentation inaccurate or is this a semantic issue since the attribute is apparently used by the compiler but not the framework?)

like image 843
D Stanley Avatar asked Nov 06 '12 22:11

D Stanley


People also ask

What is the specialnameattribute used for?

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.

What is the difference between the name attribute and Param attribute?

For the meta element, the name attribute specifies a name for the information/value of the content attribute. For the param element, the name attribute is used together with the value attribute to specify parameters for the plugin specified with the <object> tag.

What is the use of the name attribute of an element?

The name attribute specifies a name for the element. This name attribute can be used to reference the element in a JavaScript. For iframe element it can be used to target a form submission.

How do I target form submissions with the name attribute?

For an <iframe> element, the name attribute can be used to target a form submission. For a <map> element, the name attribute is associated with the <img> 's usemap attribute and creates a relationship between the image and the map.


1 Answers

As you can tell when looking at the generated IL from that code, the compiler uses the attribute to turn on the specialname IL attribute on the method declaration. The best place to look for its use is the ECMA 335 CLI specification.

In general, it is used to mark the identifier as having special usage. You'll see it turn on for example on the getter and setter method of a property, event accessor methods and operator overloads. Note the distinction with the rtspecialname IL attribute, the specialname attribute affects tools and not the jitter. Like the VB.NET compiler in this case which now distinguishes op_Exponent() as an operator overload instead of just a plain method with a funny name.

Do note that this is all baked-in behavior, you can't pull tricks that the compiler and jitter were not programmed to recognize.

like image 123
Hans Passant Avatar answered Sep 21 '22 21:09

Hans Passant