Click here to Skip to main content
Click here to Skip to main content

Retrieving Assembly Attributes

, 18 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Helper class for easy retrieving of assembly attributes.

Introduction

AssemblyAttributes is a cute little helper class that is useful for almost every .NET/Silverlight application that displays some information about itself (think “About Box”). It retrieves the values of the following assembly attributes in an easy and consistent manner:

  • Title
  • Product
  • Copyright
  • Company
  • Description
  • Trademark
  • Configuration
  • Version
  • FileVersion
  • InformationalVersion

Doing so is a piece of cake for every experienced developer, but getting these information for the 100th time, manually, is quite cumbersome. And, junior developers sometimes struggle with Reflection to get these attributes. With the help of Generics and Lambda Expressions, you can code an elegant class that solves this problem once and for all.

Using the Code

Create an instance of the AssemblyAttributes class and query its properties:

AssemblyAttributes assembly = new AssemblyAttributes();

Console.WriteLine("Title: " + assembly.Title);
Console.WriteLine("Product: " + assembly.Product);
Console.WriteLine("Version: " + assembly.Version);

You can give the constructor an explicit assembly like this:

var assembly = new AssemblyAttributes(Assembly.GetEntryAssembly());

Points of Interest

The implementation of each attribute property is elegant, thanks to Generics and lambdas:

public string Title 
{
    get { return GetValue<AssemblyTitleAttribute>(a => a.Title); } 

}

The real workhorse of this class is the GetValue method. It gets a generic custom attribute from an assembly. If it exists, it returns the result of the getValue delegate on it. If the attributes does not exist, it returns the empty string.

string GetValue<T>(Func<T, string> getValue) where T : Attribute
{
    T a = (T)Attribute.GetCustomAttribute(_assembly, typeof(T));
    return a == null ? "" : getValue(a);
}

Note the workaround in the implementation of the Version property, needed for Silverlight 3, because the GetName() method is marked as security_critical and cannot be called by user code.

public string Version
{
    get
    {
#if !SILVERLIGHT
        return _assembly.GetName().Version.ToString(); 
#else
        return _assembly.FullName.Split(',')[1].Split('=')[1]; 
#endif
     }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Christian Rodemeyer
Architect Kabel Deutschland
Germany Germany
I'm developing for fun since 1985, starting with UCSD Pascal on some old machines (no harddisk, but four floppies!), then moving quickly on to assembler on the famous C64 and Amiga. During university I started professional development for Windows/Unix/Linux, using a myriad of languages (Pi, 386/486, Cobol, Modula2, OML, C, C++, VB, Prolog, Eiffel, Delphi, Perl, Pascal, Assembler). Currently my favorite languages are C# 3.0 and Python.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411019.1 | Last Updated 18 Dec 2009
Article Copyright 2009 by Christian Rodemeyer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid