This multi-part article answers common questions about assemblies, the basic building blocks of .NET applications. This Part 2 discusses assembly attributes.

What are assembly attributes?

Assembly attributes are values (typically set by the developer) that provide additional information about a .NET assembly. Assembly attributes are grouped as follows:

  • Identity Attributes – Determine the identity of an assembly: name, version, culture and flags.
  • Informational Attributes – Provide additional information about an assembly: company name, product name, copyright, trademark, and file version.
  • Manifest Attributes – Provide information in the assembly manifest: assembly title, description, alias, and configuration (such as debug or release).
  • Strong Name Attributes – Used to help set an assembly’s strong name, including key file, key name, and whether delay signing is used.

How do I set assembly attributes?

There are two ways to set the attributes for an assembly in your development project. Using Visual Studio 2005:

Option 1: AssemblyInfo File

In the Visual Studio Solution Explorer, navigate to the Properties folder, then open the AssemblyInfo.cs file. You can directly edit the attributes in the AssemblyInfo file:


// General information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle( "MyAssembly" )]
[assembly: AssemblyDescription( "Description of MyAssembly" )]
[assembly: AssemblyConfiguration( "" )]
[assembly: AssemblyCompany( "MyCompany" )]
[assembly: AssemblyProduct( "MyProduct" )]
[assembly: AssemblyCopyright( "Copyright (c) 2007 MyCompany Inc" )]
[assembly: AssemblyTrademark( "MyProduct is a trademark of MyCompany Inc." )]
[assembly: AssemblyCulture( "" )]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible( false )]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid( "b7bfd909-f048-4d5b-9f33-1d0642398e4f" )]

// Version information for an assembly consists of the following four values:
//      Major Version
//      Minor Version
//      Build Number
//      Revision
[assembly: AssemblyVersion( "1.0.0.*" )]
[assembly: AssemblyFileVersion( "1.0.0.0" )]

Option 2: Project Properties

Select your assembly project in the Visual Studio Solution Explorer. Click the Properties button. Select the Application tab. Then click the Assembly Information button. The following dialog will appear, allowing you to edit the assembly attributes:

AssemblyInformation

How do I access a loaded assembly?

When working with assemblies, be sure to include the Reflection namespace:

using System.Reflection;

To access your application’s main assembly (i.e., the executable file):

Assembly asm = Assembly.GetExecutingAssembly();

To access an external assembly loaded by your application, call GetAssembly with a type defined in the external assembly:

Assembly asm = Assembly.GetAssembly( typeof( MyType ) );

How do I retrieve assembly attributes programmatically?

Once you have the Assembly object as shown above, you can obtain its identity attributes from its associated AssemblyName object:

AssemblyName asmName = asm.GetName();
Console.WriteLine( "Name={0}, Version={1}, Culture={2}, ProcessorArchitecture={3}",
    asmName.Name, asmName.Version, asmName.CultureInfo, asmName.ProcessorArchitecture );

For all other assembly attributes, you can load the attribute directly with the GetCustomAttributes method:

// assembly description attribute
string asmDesc = ((AssemblyDescriptionAttribute)asm.GetCustomAttributes(
    typeof( AssemblyDescriptionAttribute ), false )[0]).Description;
// assembly title attribute
string asmTitle = ((AssemblyTitleAttribute)asm.GetCustomAttributes(
    typeof( AssemblyTitleAttribute ), false )[0]).Title;
// etc.

To view all attributes for an assembly:

object[] attributes = asm.GetCustomAttributes( true );
foreach (object obj in attributes)
{
    Console.WriteLine( obj.ToString() );
}

.NET Assembly FAQ – Part 1
.NET Assembly FAQ – Part 3 – Strong Names and Signing