Click here to Skip to main content
15,884,176 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 
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.