Creating and Reading Global Attributes in C#
By FoxLearn 12/26/2024 7:47:24 AM 9
This file uses global attributes to store critical information about the assembly, such as its title, version, and copyright.
For example:
// 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("GlobalAttributesExample")] [assembly: AssemblyDescription("This is a sample assembly.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("My Company")] [assembly: AssemblyProduct("GlobalAttributesExample Product")] [assembly: AssemblyCopyright("Copyright © 2024")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")]
To create custom global attributes, you first need to define a class that extends the Attribute
base class, which will store your specific metadata.
Creating a Custom Global Attribute
The class will have a single property to hold a string value, which could represent any piece of metadata you'd like to store.
using System; namespace GlobalAttributesExample { public class MyCustomAttribute : Attribute { public string MyValue { get; set; } } }
This basic custom attribute stores a string value.
Applying Global Attributes to Assembly and Module
In this example, we will apply the custom attribute to both the assembly and module of our program. Global attributes are applied just like method or class-level attributes, but the scope is specified as either assembly
or module
.
using System; using GlobalAttributesExample; [assembly: MyCustomAttribute(MyValue = "This is the assembly attribute.")] [module: MyCustomAttribute(MyValue = "This is the module attribute.")] namespace GlobalAttributesExample { class Program { static void Main(string[] args) { // Code to read the attribute values will go here } } }
Here, we apply MyCustomAttribute
to both the assembly and the module. The MyValue
property holds a string with the corresponding attribute information.
Reading Global Attributes at Runtime
First, we need to retrieve the Assembly
object, and then we can call GetCustomAttributes()
to get the custom attributes applied to the assembly and module.
using System; using System.Reflection; using GlobalAttributesExample; [assembly: MyCustomAttribute(MyValue = "This is the assembly attribute.")] [module: MyCustomAttribute(MyValue = "This is the module attribute.")] namespace GlobalAttributesExample { class Program { static void Main(string[] args) { // Reading assembly-level attributes object[] assemblyAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes( typeof(MyCustomAttribute), false); MyCustomAttribute assemblyAttr = assemblyAttributes[0] as MyCustomAttribute; Console.WriteLine("Assembly Attribute: " + assemblyAttr.MyValue); // Reading module-level attributes object[] moduleAttributes = typeof(Program).Module.GetCustomAttributes( typeof(MyCustomAttribute), false); MyCustomAttribute moduleAttr = moduleAttributes[0] as MyCustomAttribute; Console.WriteLine("Module Attribute: " + moduleAttr.MyValue); Console.WriteLine("Press enter to exit."); Console.ReadLine(); } } }
In this code, we first retrieve the attributes for the assembly and then for the module containing the Program
class. We use Assembly.GetExecutingAssembly()
to get the current assembly, and typeof(Program).Module
to get the module for the Program
class.
When running this code, the output will display the values from the custom attributes we applied to both the assembly and the module.
Assembly Attribute: This is the assembly attribute. Module Attribute: This is the module attribute. Press enter to exit.
This tutorial demonstrated how to create and read custom global attributes in C#.