C# - Creating and Reading Global Attributes [Beginner]


Global attributes aren't something that many C# developers use often, however almost every application contains them, whether you're aware of them or not. Global attributes are a great way to store meta information for a specific assembly or module.

Global attributes are split into two categories: assembly and module. A module is a single code file and an assembly is a single DLL or executable. An assembly can contain several modules and a module can contain several classes.

When you create a new project in Visual Studio, usually it creates a file for you called AssemblyInfo.cs. This file uses global attributes to store important information about the assembly like version and copyright information. Below is an excerpt from an example AssemblyInfo.cs 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("GlobalAttributes")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GlobalAttributes")]
[assembly: AssemblyCopyright("Copyright ©  2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
 
In order to create our own custom attributes like these, the first thing we're going to need to do is create a class that extends Attribute to store the information.
using System;

namespace GlobalAttributes
{
  public class MyCustomAttribute : Attribute
  {
    public string MyValue { get; set; }
  }
}
 
Here I created a very basic custom attribute. It has one property, MyValue, which holds a string. Now let's see how to use the attribute in our own code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalAttributes;

[assembly: MyCustomAttribute(MyValue="My assembly attribute value.")]
[module: MyCustomAttribute(MyValue = "My module attribute value.")]

namespace GlobalAttributes
{
  class Program
  {
    static void Main(string[] args)
    {

    }
  }
}
 
I started with a default console application and added the global attribute below the using statements and above the namespace declaration. Global attributes are created exactly like the more common method and class level attributes, except the scope is defined as either assembly or module.

Now that we've got some information stored in our custom global attributes, let's now look at how to read it at run-time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalAttributes;
using System.Reflection;

[assembly: MyCustomAttribute(MyValue="My assembly attribute value.")]
[module: MyCustomAttribute(MyValue = "My module attribute value.")]

namespace GlobalAttributes
{
  class Program
  {
    static void Main(string[] args)
    {
      //Get all the attributes with the type MyCustomAttribute
      object[] attributes = 
        Assembly.GetExecutingAssembly().GetCustomAttributes(
        typeof(MyCustomAttribute), false);

      //Get the first (and only) entry and print the value
      MyCustomAttribute attr = attributes[0] as MyCustomAttribute;
      Console.WriteLine(attr.MyValue);

      Console.WriteLine("Press enter to exit.");
      Console.ReadLine();
    }
  }
}
 
The first thing we need to do is actually get an Assembly object. There's lots of ways to do this, but the easiest is to simply get the currently executing one. We then call GetCustomAttributes and pass it the type of the attribute we're interested in. The boolean parameter is actually ignored by .NET when called on Assembly objects, so set it to whatever you want. I know that there's only one of these attributes applied to the assembly so I just grab the first one out and cast it to the correct type. Last, just print the value. Here's the console output of this code:
My assembly attribute value.
Press enter to exit.
Next up is module attributes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalAttributes;
using System.Reflection;

[assembly: MyCustomAttribute(MyValue="My assembly attribute value.")]
[module: MyCustomAttribute(MyValue = "My module attribute value.")]

namespace GlobalAttributes
{
  class Program
  {
    static void Main(string[] args)
    {
      //Get all the attributes with the type MyCustomAttribute
      object[] attributes = 
        Assembly.GetExecutingAssembly().GetCustomAttributes(
        typeof(MyCustomAttribute), false);

      //Get the first (and only) entry and print the value
      MyCustomAttribute attr = attributes[0] as MyCustomAttribute;
      Console.WriteLine(attr.MyValue);

      //Get the attributes for the module containing the Program class
      attributes = typeof(Program).Module.GetCustomAttributes(
        typeof(MyCustomAttribute), false);

      //Get the first (and only) entry and print the value
      attr = attributes[0] as MyCustomAttribute;
      Console.WriteLine(attr.MyValue);

      Console.WriteLine("Press enter to exit.");
      Console.ReadLine();
    }
  }
}
 
Each object will belong to one Module. I get a reference to the module containing the class Program by using the Module property on the Type class. The rest of the code is exactly like assembly attributes and again, the boolean parameter to GetCustomAttributes is ignored by .NET. Here's the output of this code:
My assembly attribute value.
My module attribute value.
Press enter to exit.
 
That's it. This tutorial demonstrated how to create and read global attributes in C#. If you've got any questions, feel free to leave them below.

Comments