This is a mirror of official site: http://jasper-net.blogspot.com/

Subterranean IL: Custom modifiers

| Sunday, November 28, 2010
In IL, volatile is an instruction prefix used to set a memory barrier at that instruction. However, in C#, volatile is applied to a field to indicate that all accesses on that field should be prefixed with volatile. As I mentioned in my previous post, this means that the field definition needs to store this information somehow, as such a field could be accessed from another assembly. However, IL does not have a concept of a 'volatile field'. How is this information stored?

Attributes

The standard way of solving this is to apply a VolatileAttribute or similar to the field; this extra metadata notifies the C# compiler that all loads and stores to that field should use the volatile prefix. However, there is a problem with this approach, namely, the .NET C++ compiler. C++ allows methods to be overloaded using properties, like volatile or const, on the parameters; this is perfectly legal C++:

public ref class VolatileMethods {
   void Method(int *i) {}
   void Method(volatile int *i) {}
}


If volatile was specified using a custom attribute, then the VolatileMethods class wouldn't be compilable to IL, as there is nothing to differentiate the two methods from each other. This is where custom modifiers come in.
Custom modifiers

Custom modifiers are similar to custom attributes, but instead of being applied to an IL element separately to its declaration, they are embedded within the field or parameter's type signature itself. The VolatileMethods class would be compiled to the following IL:

.class public VolatileMethods {
   .method public instance void Method(int32* i) {}
   .method public instance void Method(
       int32 modreq(
       [mscorlib]System.Runtime.CompilerServices.IsVolatile)* i) {}
}


The modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile) is the custom modifier. This adds a TypeDef or TypeRef token to the signature of the field or parameter, and even though they are mostly ignored by the CLR when it's executing the program, this allows methods and fields to be overloaded in ways that wouldn't be allowed using attributes.

Read more: Simple-talk

Posted via email from .NET Info

2 comments:

Anonymous said...

I wantеԁ to thank уou fоr thіs fantaѕtіc read!
! I definitely enjoyed everу bit of it. I have got you boоk marked to сheck out
new stuff yοu post…

Also visit my web page ... bike rental austin

Anonymous said...

Hellо just wanted tо gіѵe you a quick heaԁs up.
The words іn уouг post ѕeеm
to be гunning off the screen іn Opera. I'm not sure if this is a formatting issue or something to do with web browser compatibility but I thought I'd post to
let уou know. The design look great though!
Hope you get the issue sοlved soon. Many thanks

Heгe is my website transaq.ru