I stumbled upon this recently and wanted to share. As you may be aware, attributes provide a mechanism to describe an assembly, class, method, etc with information known as metadata. This metadata is useful alter behaviors (e.g. AutoCompleteAttribute), define security restrictions (e.g. FileIOPermissionAttribute), enforce compile-time analyses (e.g. ObsoleteAttribute, CLSCompliantAttribute, AttributeUsageAttribute) or simply provide information (e.g. AssemblyTitleAttribute, AssemblyFileVersionAttribute, AssemblyCopyrightAttribute).
Via reflection you can query a target such as an assembly, et al to see what attributes it has applied. A simple, but somewhat useless example might resemble the following:
using System;
using System.Reflection;
object[] attribs = Assembly.GetExecutingAssembly().GetCustomAttributes(false);
foreach ( Attribute a in attribs ) {
Console.WriteLine(a.GetType().FullName);
}
It simply spits out the attributes associated with the running assembly. It's pretty cool, and there are some applications that take advantage of such functionality.
Most likely you've had the opportunity to version stamp an assembly with the AssemblyVersionAttribute. If so, your code probably resembled:
[assembly: AssemblyVersion(”1.0.0.0”)]
If you then run the above code on an assembly so versioned, you might be surprised not to see it there. I don't fully understand (nor have I thought much about it, nor investigated) the reasoning behind this. If you want to extract the version from the assembly you have to go an alternative route. Instead, you call the GetName() method on the Assembly to retrieve an AssemblyName object which contains a Version class. A simple call to .ToString() on the Version will yield the property value.
using System;
using System.Reflection;
Assembly asm = Assembly.GetExecutingAssembly();
Version ver = asm.GetName().Version;
Console.WriteLine(”Version “ + ver.ToString());
Does anyone know of any other attributes that don't translate into metadata such as this? Am I wrong and it's really there but I just don't see it or am not querying for it properly?