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

Tagged as

Go to top

Get all Assembly Information

, 27 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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, null will be returned.

Usage

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

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

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)

Share

About the Author

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

Comments and Discussions

 
Suggestionnamespace screwed up, remove the static members... PinmemberAndreas Gieriet25-Mar-12 17:17 
GeneralRe: namespace screwed up, remove the static members... PinmemberHenning Dieterichs25-Mar-12 20:50 
Thank you for your suggestions!
I can see your point with the assembly name, but actually I have no company (I'll think about it) Wink | ;)
To the static functions / properties:
The static property "CallingAssembly" actually won't be cached, and I admit, a property is senseless in this place (whereas a static method could be senseful).
I wouldn't agree that the property "EntryAssembly" is senseless too, as the purpose of this class is an easy access to all assembly information (and when providing such an property it must be cached - but why do you think caching in this case is bad?).
When it comes to WPF (e.g. an about-box), it would be hard in XAML to get an instance of the AssemblyInformation class if there wasn't the static property... (either you define it in the code behind (which is in my opinion very bad) or you try to call the constructor of AssemblyInformation with the result of a static method-call of the assembly-class (which is btw. only possible with the class ObjectDataProvider ) - quite cumbersome)
 
And I don't understand you point referring to the speed impact: When there is no performance impact, why it won't be used where speed matters?
 
Greetings,
Henning
If you find spelling- or grammer-mistakes, please let me know, so that I can correct them (at least for me) - english is not my first language...

GeneralRe: namespace screwed up, remove the static members... [modified] PinmemberAndreas Gieriet25-Mar-12 21:43 
GeneralRe: namespace screwed up, remove the static members... PinmemberHenning Dieterichs27-Mar-12 5:18 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 27 Mar 2012
Article Copyright 2012 by Henning Dieterichs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid