Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#
Tip/Trick

Get all Assembly Information

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
27 Mar 2012CDDL 66.2K   9   5
This tip shows how to get quickly all Information of an Assembly

Introduction

With assembly attributes it is possible to specify assembly meta information such as product title or version, either automatically by Visual Studio (see here for more information) or manually by editing the file "AssemblyInfo.cs" in the project folder "Properties".

With the class provided with this tip these attributes can be easily read from any assembly.

To obtain information of an assembly the constructor of AssemblyInfo can be used by passing the specific assembly.

If the corresponding assembly attribute to a property is not found, <code>null will be returned.

Usage

This example will write all assembly information of the entry assembly to the console.

C#
AssemblyInfo entryAssemblyInfo = new AssemblyInfo(Assembly.GetEntryAssembly());
Console.WriteLine("Company: " + entryAssemblyInfo.Company);
Console.WriteLine("Copyright: " + entryAssemblyInfo.Copyright);
Console.WriteLine("Description: " + entryAssemblyInfo.Description);
Console.WriteLine("Product: " + entryAssemblyInfo.Product);
Console.WriteLine("ProductTitle: " + entryAssemblyInfo.ProductTitle);
Console.WriteLine("Version: " + entryAssemblyInfo.Version);

The Class

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace HdSystemLibrary.Reflection
{
    public class AssemblyInfo
    {
        public AssemblyInfo(Assembly assembly)
        {
            if (assembly == null)
                throw new ArgumentNullException("assembly");
            this.assembly = assembly;
        }

        private readonly Assembly assembly;

        /// <summary>
        /// Gets the title property
        /// </summary>
        public string ProductTitle
        {
            get
            {
                return GetAttributeValue<AssemblyTitleAttribute>(a => a.Title, 
                       Path.GetFileNameWithoutExtension(assembly.CodeBase));
            }
        }

        /// <summary>
        /// Gets the application's version
        /// </summary>
        public string Version
        {
            get
            {
                string result = string.Empty;
                Version version = assembly.GetName().Version;
                if (version != null)
                    return version.ToString();
                else
                    return "1.0.0.0";
            }
        }

        /// <summary>
        /// Gets the description about the application.
        /// </summary>
        public string Description
        {
            get { return GetAttributeValue<AssemblyDescriptionAttribute>(a => a.Description); }
        }


        /// <summary>
        ///  Gets the product's full name.
        /// </summary>
        public string Product
        {
            get { return GetAttributeValue<AssemblyProductAttribute>(a => a.Product); }
        }

        /// <summary>
        /// Gets the copyright information for the product.
        /// </summary>
        public string Copyright
        {
            get { return GetAttributeValue<AssemblyCopyrightAttribute>(a => a.Copyright); }
        }

        /// <summary>
        /// Gets the company information for the product.
        /// </summary>
        public string Company
        {
            get { return GetAttributeValue<AssemblyCompanyAttribute>(a => a.Company); }
        }

        protected string GetAttributeValue<TAttr>(Func<TAttr, 
          string> resolveFunc, string defaultResult = null) where TAttr : Attribute
        {
            object[] attributes = assembly.GetCustomAttributes(typeof(TAttr), false);
            if (attributes.Length > 0)
                return resolveFunc((TAttr)attributes[0]);
            else
                return defaultResult;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Student
Germany Germany
Presently I am a student of computer science at the Karlsruhe Institute of Technology in Germany.

Comments and Discussions

 
QuestionRegarding full workspace Pin
Member 114944793-Mar-15 2:01
Member 114944793-Mar-15 2:01 
Suggestionnamespace screwed up, remove the static members... Pin
Andreas Gieriet25-Mar-12 17:17
professionalAndreas Gieriet25-Mar-12 17:17 
GeneralRe: namespace screwed up, remove the static members... Pin
Henning Dieterichs25-Mar-12 20:50
Henning Dieterichs25-Mar-12 20:50 
GeneralRe: namespace screwed up, remove the static members... Pin
Andreas Gieriet25-Mar-12 21:43
professionalAndreas Gieriet25-Mar-12 21:43 
Hello Henning,


  • namespace: call it namespace HenningsLibrary.Utilities; or alike. BTW: the namespace is not necessarily the same as the assembly name (as you name it in your reply).
  • cacheing: you are right, the CallingAssembly is not cached, but the EntryAssembly is.
  • static functions: I strongly recommend to decouple the intended use in WPF from the bare AssemblyInfo implementation. If you want to use it in WPF (XAML), then I would use the MVVM pattern and let the ViewModel deal with the quirks of connecting to the View (e.g. make an AssemblyInfoCommand that delives the desired information by instanciating the AssemblyInfo class and get the data from there).
  • performance: "simplicity over performance": someone has to maintain your code. E.g. without the (cacheing) static function, you don't have to care about thread safety. You do not gain anything adding this complexity, since one can assume that calling this AssemblyInfo class is done in completely uncritical circumstances. I have a quite extreme position: each unnecessary line of code is one too much - it bares the risk of error. It makes the class unnecessarily complicated, especially since the motivation for the static access belongs to some other level (i.e., into the VM of the MVVM).


BTW: to XAML and constructor arguments see:
- http://www.wpftutorial.net/XAML2009.html[^]
- http://msdn.microsoft.com/en-us/library/ee795382.aspx[^]
- http://stackoverflow.com/questions/1083159/calling-a-parametrized-constructor-from-xaml[^]

Cheers
Andi

modified 26-Mar-12 3:51am.

GeneralRe: namespace screwed up, remove the static members... Pin
Henning Dieterichs27-Mar-12 5:18
Henning Dieterichs27-Mar-12 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.