Monday, June 11, 2007

Using a global ProductInfo.cs file

When a project gets beyond a certain size, it becomes impossible to keep the multitude of AssemblyInfo.cs files up do date, particularly for copyright and version numbers.

The solution to this problem is to use a single ProductInfo.cs file that every project links to, which contains the common data that would normally be duplicated across many AssemblyInfo.cs files.

Here are the contents of ProductInfo.cs

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Runtime.InteropServices;

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("My Company Limited")]

[assembly: AssemblyProduct("My Product")]

[assembly: AssemblyCopyright("Copyright © 2006-2007")]

[assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]

[assembly: AssemblyInformationalVersion("v1.0 alpha")]

// [assembly: AssemblyKeyFile("MyCodeKey.snk")]

 This allows the AssemblyInfo.cs file for any assembly to be pared down to the following:

using System;

using System.Reflection;

using System.Runtime.InteropServices;

[assembly: AssemblyTitle("MyCompany.MyProduct.MyAssembly")]

[assembly: AssemblyDescription("Description of MyAssembly")]

[assembly: ComVisible(false)]

[assembly: CLSCompliant(true)]

[assembly: Guid("b76c311c-f681-48d1-b2e0-9f691feb239f")]

[assembly: AssemblyVersion("1.0.0.0")]

[assembly: AssemblyFileVersion("1.0.0.0")]

There are 2 ways to add a link to the ProductInfo.cs to your projects:

1.Drag and drop (the easy option) 

This method simply entails dragging the linked ProductInfo.cs from an existing project and dropping it the Properties folder of your project.

2.Add Existing item (an alternative option)

This method includes add an existing item to your project, by right clicking on the project file and selecting Add -> Existing Item from the context menu. Then navigate to the directory containing the ProductInfo.cs file and select the ProductInfo.cs file. Finally (and  this is the clever bit) select the Add Link option from the dropdown attached to the Add button.

I tend to add a solution (virtual) folder called "Solution Items" to each large solution I'm working on and add the ProductInfo.cs file to that. I can then easily drag-and-drop as needed.

When this technique is coupled with automated Assembly versioning, your live becomes much easier!