David Brown

The Web Development Guy
posts - 5, comments - 19, trackbacks - 0

My Links

News

View David Brown's profile on LinkedIn

Twitter












Tag Cloud

Archives

Easy Access to AssemblyInfo.cs attributes

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.

Print | posted on Monday, April 06, 2009 3:38 PM |

Feedback

Gravatar

# re: Easy Access to AssemblyInfo.cs attributes

Thanks, David. Just what I needed.
6/19/2009 10:38 AM | Scott Marlowe
Gravatar

# re: Easy Access to AssemblyInfo.cs attributes

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!
10/6/2009 11:45 PM | si
Gravatar

# re: Easy Access to AssemblyInfo.cs attributes

opps...you're doing exactly that in the link.....mea culpa :)
10/7/2009 12:42 AM | si
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: