Geeks With Blogs
David Brown The Web Development Guy

For my current project, I've implemented a Shared AssemblyInfo.cs file, mostly because every project is from the same company and share some common version numbers. In one of my console applications, I needed to construct a header using the Assembly Title and Copyright information from my SharedAssemblyInfo.cs file. To achieve this, I added the following to the end of SharedAssemblyInfo.cs:

namespace MyNamespace {
    public static class AssemblyInfo {
        public static string Title {
            get {
                var attr = GetAssemblyAttribute<AssemblyTitleAttribute>();
                if (attr != null)
                    return attr.Title;
                return string.Empty;
            }
        }

        public static string Company {
            get {
                var attr = GetAssemblyAttribute<AssemblyCompanyAttribute>();
                if (attr != null)
                    return attr.Company;
                return string.Empty;
            }
        }

        public static string Copyright {
            get {
                var attr = GetAssemblyAttribute<AssemblyCopyrightAttribute>();
                if (attr != null)
                    return attr.Copyright;
                return string.Empty;
            }
        }

        private static T GetAssemblyAttribute<T>() where T : Attribute {
            object[] attributes = Assembly.GetExecutingAssembly()
                .GetCustomAttributes(typeof(T), true);
            if (attributes == null || attributes.Length == 0) return null;
            return (T)attributes[0];
        }
    }
}

Now, I can use it like this:

Console.WriteLine(AssemblyInfo.Title);
Console.WriteLine(AssemblyInfo.Copyright);

The best part is that since SharedAssemblyInfo.cs is linked to each of my projects, I can use the AssemblyInfo class wherever I want!

Also, the class is reading attributes from the assembly that's currently executing, which means that if you call AssemblyInfo.Title in Project 1, it will return the value of Project 1's AssemblyTitleAttribute, while Project 2 will return its own value. If you have defined the assembly title in a linked file, however, both Project 1 and Project 2 will return that value.

Posted on Monday, April 6, 2009 3:38 PM | Back to top


Comments on this post: Easy Access to AssemblyInfo.cs attributes

# re: Easy Access to AssemblyInfo.cs attributes
Requesting Gravatar...
Thanks, David. Just what I needed.
Left by Scott Marlowe on Jun 19, 2009 10:38 AM

# re: Easy Access to AssemblyInfo.cs attributes
Requesting Gravatar...
Another (imho easier) way is to just have different assembly info files. Then for each project, add your common assembly info files using "Add as link" instead of the typical add. The only cleanup I do is move the shortcut into properties.

We typically have 3 assembly info files:

AssemblyInfo.cs -> attributes specific to assembly
ProductAssemblyInfo.cs -> attributes specific to product
CompanyAssemblyInfo.cs -> you get the drift :)

Works a treat!
Left by si on Oct 06, 2009 11:45 PM

# re: Easy Access to AssemblyInfo.cs attributes
Requesting Gravatar...
opps...you're doing exactly that in the link.....mea culpa :)
Left by si on Oct 07, 2009 12:42 AM

Your comment:
 (will show your gravatar)


Copyright © David Brown | Powered by: GeeksWithBlogs.net