Recent site activity

C# - C Sharp‎ > ‎Tips and Trick‎ > ‎

Share a GlobalAssemblyInfo.cs file among projects in a solution

Contents

  1. 1 Steps

Applies to Visual Studio .NET 2003/2005

When create a project in Visual Studio, the "AssemblyInfo.cs" file will automatically created too. It describes the assembly generated by the project.

Generally, there are several projects in a solution, each of which has a "AssemblyInfo.cs" file. However, the code in these "AssemblyInfo.cs" are almost the same. It is so redundant. As we know, a great number of bugs are caused by the duplicated code. Because when modifying source code, it is difficult to ensure that every piece of code has already been updated. Let's take the "AssemblyInfo.cs" as a example. We have 20 projects in a solution. Our boss asked us to change the assemblies' version. Then we have to modify all the 20 files, and we must assure that all of these must be updated. It isn't a nice job for us, I think. So we'd better extra all the duplicated code into a global file, which will be shared by all the projects.

Steps

  • Create a file called "GlobalAssemblyInfo.cs";
    • using System;
    • using System.Reflection;
    • using System.Runtime.InteropServices;
    •  
    • [assembly : ComVisible(false)]
    • [assembly : CLSCompliant(true)]
    •  
    • [assembly : AssemblyProduct("my product")]
    • [assembly : AssemblyCompany("my company")]
    • [assembly : AssemblyCopyright("\x00a9 my company. All rights reserved.")]
    •  
    • [assembly : AssemblyVersion("1.0.0.0")]
    •  
    • #if DEBUG
    • [assembly : AssemblyConfiguration("Debug")]
    • #else
    • [assembly : AssemblyConfiguration("Release")]
    • #endif
  • Add the file to the solution as a solution item;
  • Add the "GlobalAssemblyInfo.cs" to the projects as a link file. You can use one of the following two methods to add a link file.
    • Open a project file(.csproj, etc.) in a text editor,
      Visual Studio 2003: appends the following xml content to the <Files>/<Include> element.
      • <File
      • RelPath = "GlobalAssemblyInfo.cs"
      • Link = "relative path to GlobalAssemblyInfo.cs"
      • SubType = "Code"
      • BuildAction = "Compile" />


      Visual Studio 2005: appends the following xml content to the <Project>/<ItemGroup> element.
      • <Compile Include="..\GlobalAssemblyInfo.cs">
      • <Link>Properties\GlobalAssemblyInfo.cs</Link>
      • <SubType>Code</SubType>
      • </Compile>
    • Right click a project's title and select Add Existing Item. Browse to the "GlobalAssemblyInfo.cs", and on the Open button click on the little down arrow on the button and select Link File.