# Thursday, 08 March 2012
« Lang.NEXT | Main | New Development Snapshot »
CLR Supported Custom Attributes

After working with the CLR for more than a decade, once in a while I still run into surprising behavior.

It turns out that the (non pseudo-) custom attributes that CLR recognizes are only matched by name, not assembly.

So you can do this for example:

using System;
using System.Threading;

namespace System {
  class ThreadStaticAttribute : global::System.Attribute { }
}

class Program {
  [System.ThreadStaticAttribute]
  static int foo;

  public static void Main() {
    WriteFoo();
    foo = 42;
    WriteFoo();
    new Thread(WriteFoo).Start();
  }

  static void WriteFoo() {
    Console.WriteLine(foo);
  }
}

On the CLR the foo static variable is a thread local, but on Mono it isn't.

Thursday, 08 March 2012 14:35:51 (W. Europe Standard Time, UTC+01:00)  #    Comments [5]
Friday, 09 March 2012 13:20:47 (W. Europe Standard Time, UTC+01:00)
So just to check, in the above case the compiler writes to the assembly that the attribute is a class of name <N> in <this assembly> but the MSFT runtime ignores the assembly and loads a class with that name from first assembly in its list of known assemblies that has one?
Alan McFarlane
Friday, 09 March 2012 13:51:36 (W. Europe Standard Time, UTC+01:00)
No, when you use reflection to access the custom attribute, you'll get back the right type (the one from the current assembly). It's just that the CLR recognizes some special custom attributes (like System.ThreadStaticAttribute), but does this without actually resolving the type, it just looks at the type name.
Friday, 09 March 2012 19:37:41 (W. Europe Standard Time, UTC+01:00)
Looks like someone did a hack there because he couldn't directly think of the right way to get the ThreadStatic type for a comparison.
tobi
Tuesday, 13 March 2012 12:24:21 (W. Europe Standard Time, UTC+01:00)
Ahh ok. My suggestion was a bit far fetched!

Another one that is apparently be matched by name alone is ExtensionAttribute, of course by the compiler, see e.g. http://www.danielmoth.com/Blog/Using-Extension-Methods-In-Fx-20-Projects.aspx I thought there was another one the Compact Framework MVPs described as working that way but maybe it was that one too.

Alan
Alan McFarlane
Tuesday, 13 March 2012 13:07:18 (W. Europe Standard Time, UTC+01:00)
There are a bunch of them. Here's partial list based on the Shared Source CLI:

System.LoaderOptimizationAttribute
System.Runtime.CompilerServices.CompilationRelaxationsAttribute
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Runtime.CompilerServices.UnsafeValueTypeAttribute
System.ContextStaticAttribute
System.Runtime.CompilerServices.FixedAddressValueTypeAttribute
System.Security.SuppressUnmanagedCodeSecurityAttribute
System.Runtime.Serialization.OptionalFieldAttribute
System.Runtime.Serialization.OnSerializingAttribute
System.Runtime.Serialization.OnSerializedAttribute
System.Runtime.Serialization.OnDeserializingAttribute
System.Runtime.Serialization.OnDeserializedAttribute
System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute
System.Diagnostics.DebuggableAttribute
System.Runtime.InteropServices.LCIDConversionAttribute
System.Runtime.InteropServices.ComVisibleAttribute
System.Runtime.InteropServices.ComCompatibleVersionAttribute
System.Runtime.InteropServices.BestFitMappingAttribute
System.Security.SecurityCriticalAttribute
System.Security.SecurityTreatAsSafeAttribute
System.Security.AllowPartiallyTrustedCallersAttribute
System.Security.SecurityTransparentAttribute
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview