Posted by: Cirilo Meggiolaro | 11/27/2008

Tip of the day #44 – Retrieving Assembly attributes

Assembly attributes allow you to set some built-in properties on the assembly level.

To achieve that, you have to define in the AssemblyInfo file attributes that starts with the assembly keyword like the following code snippet:

[assembly: AssemblyVersion(“1.0.0.0”)]

If you check the AssemblyInfo file you will have an idea of part of the available attributes.

Let’s take a look now to use Reflection to retrieve a list of attributes defined in your application AssemblyInfo file:

/// Gets the executing assembly
Assembly myAssembly = Assembly.GetExecutingAssembly();

/// Gets the attributes type available.
object[] attributes = myAssembly.GetCustomAttributes(false);

/// If attributes have been retrieved
if ((attributes != null) && (attributes.Length > 0))
{
/// Loop through all attribute types
foreach (object attr in attributes)
{
/// Display attribute type
Console.WriteLine(“Attribute type: {0}”, attr.GetType().Name);
}
}

Output:

Attribute type: AssemblyTitleAttribute
Attribute type: AssemblyDescriptionAttribute
Attribute type: AssemblyConfigurationAttribute
Attribute type: AssemblyCompanyAttribute
Attribute type: AssemblyProductAttribute
Attribute type: AssemblyCopyrightAttribute
Attribute type: AssemblyTrademarkAttribute
Attribute type: ComVisibleAttribute
Attribute type: GuidAttribute
Attribute type: AssemblyFileVersionAttribute
Attribute type: DebuggableAttribute
Attribute type: CompilationRelaxationsAttribute
Attribute type: RuntimeCompatibilityAttribute

If you want to get the value for a specific attribute, you’ll have to instantiate the typed object to get the attribute value. The following example retrieves the assembly version:

/// Gets the executing assembly
Assembly myAssembly = Assembly.GetExecutingAssembly();

/// Gets the attribute type
Type attrType = typeof(AssemblyFileVersionAttribute);

/// Gets the attribute for the specific type
object[] attributes = myAssembly.GetCustomAttributes(attrType, false);

/// If attributes have been retrieved
if ((attributes != null) && (attributes.Length > 0))
{
/// Create an instance of the specific attribute
AssemblyFileVersionAttribute attrValue = (AssemblyFileVersionAttribute)attributes[0];

Console.WriteLine(“Assembly Version: {0}”, attrValue.Version);
}

Output:

Assembly Version: 1.0.0.0

Advertisements

Responses

  1. Hi.

    I doing the last task by using this code:

    System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

  2. Hi Fadachi,

    You are right. It is another way to get the version and in fact it is a better way to get the version once you are getting exactly the version generated.

    Meanwhile the code snippet I presented has as goal to retrieve the exact value defined on AssemblyInfo file.

    If you define your version on AssemblyInfo file like this 1.0.0.* your code will retrieve a version for example 1.0.0.18273 while
    getting the attribute will retrieve the value 1.0.0.*.

    The purpose of this tip is to give a sample about how to retrieve assembly attributes. The version attribute is not the only assembly attribute available.

    Thanks for your comments!!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: