Click here to Skip to main content
15,893,487 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 22.1K   357   15  
Helper class for easy retrieving of assembly attributes.
using System;
using System.Linq;
using System.Reflection;

namespace Rodemeyer.Helpers
{
    /// <summary>
    /// Easy access to common Assembly attributes.
    /// </summary>
    public class AssemblyAttributes
    {
        readonly Assembly _assembly;

        public AssemblyAttributes() : this(Assembly.GetCallingAssembly())
        {}

        public AssemblyAttributes(Assembly assembly)
        {
            _assembly = assembly;
        }

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

        public string Product
        {
            get { return GetValue<AssemblyProductAttribute>(a => a.Product); }
        }

        public string Copyright
        {
            get { return GetValue<AssemblyCopyrightAttribute>(a => a.Copyright); }
        }

        public string Company
        {
            get { return GetValue<AssemblyCompanyAttribute>(a => a.Company); }
        }

        public string Description
        {
            get { return GetValue<AssemblyDescriptionAttribute>(a => a.Description); }
        }    
        
        public string Trademark
        {
            get { return GetValue<AssemblyTrademarkAttribute>(a => a.Trademark); }
        }   
        
        public string Configuration
        {
            get { return GetValue<AssemblyConfigurationAttribute>(a => a.Configuration); }
        }

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

        public string FileVersion
        {
            get { return GetValue<AssemblyFileVersionAttribute>(a => a.Version); }
        }

        public string InformationalVersion
        {
            get { return GetValue<AssemblyInformationalVersionAttribute>(a => a.InformationalVersion); }
        }

        /// <summary>
        /// Returns the value of attribute T or String.Empty if no value is available.
        /// </summary>
        string GetValue<T>(Func<T, string> getValue) where T : Attribute
        {
            T a = (T)Attribute.GetCustomAttribute(_assembly, typeof(T));
            return a == null ? "" : getValue(a);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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