Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#

Retrieving Assembly Attributes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Dec 2009CPOL1 min read 22K   355   15  
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:

C#
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:

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

Points of Interest

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

C#
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.

C#
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.

C#
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)


Written By
Software Developer
Germany Germany
I'm developing for fun since 1985, starting with UCSD Pascal on some old machines (no hard disk, 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, Assembler (6502, 68000, 80386/486), Cobol, Modula2, Prolog, OML, C, C++, C#, Java, Scala, Groovy, Clojure, VB, Eiffel, Delphi, Perl, Pascal, Javascript). Currently my favorite languages are Clojure, Ruby and modern Javascript.

Comments and Discussions

 
-- There are no messages in this forum --