|
Reflection and Attributes in C#
    
Reflection provides objects that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type,
bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
The following example demonstates the usage of Reflection concept with AttributeUsageAttribute class and GetCustomAttributes method.
In this example, EmployeeAttribute is derived from Attribute class. The EmployeeAttribute class has the four atrributes EmployeeID, Designation,
Datejoined and comment.
namespace CustomAttributes
{
// create custom attribute to be assigned to class members
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class EmployeeAttribute : System.Attribute
{
// attribute constructor for positional parameters
public EmployeeAttribute
(
int employeeID,
string designation,
string datejoined
)
{
this.EmployeeID = employeeID;
this.Designation = designation;
this.Datejoined = datejoined;
}
// accessors
public int EmployeeID;
public string Designation;
public string Datejoined;
// property for named parameter
public string Comment;
}
// ********* assign the attributes to the class ********
[EmployeeAttribute(121, "Software Engineer", "01/03/05")]
[EmployeeAttribute(107, "Director of Engineering", "01/04/05",
Comment = "Grade 3")]
public class MyTest
{
public double DoTest(double param1)
{
return param1*10;
}
}
public class Tester
{
public static void Main()
{
MyTest mm = new MyTest();
Console.WriteLine("Calling DoTest(7). Result: {0}",
mm.DoTest(7));
// get the member information and use it to
// retrieve the custom attributes
System.Reflection.MemberInfo inf = typeof(MyTest);
object[] attributes;
attributes = inf.GetCustomAttributes(
typeof(EmployeeAttribute), false);
// iterate through the attributes, retrieving the
// properties
foreach (Object attribute in attributes)
{
EmployeeAttribute bfa = (EmployeeAttribute)attribute;
Console.WriteLine("\nEmployeeID: {0}", bfa.EmployeeID);
Console.WriteLine("Designation: {0}", bfa.Designation);
Console.WriteLine("Datejoined: {0}", bfa.Datejoined);
Console.WriteLine("Comment: {0}", bfa.Comment);
}
}
}
}
|
AttributeUsageAttribute: Initializes a new instance of the AttributeUsageAttribute class with the specified list of AttributeTargets, the AllowMultiple value, and the
Inherited value. When you are defining your own attribute class, you can control the manner in which it is used by placing an AttributeUsageAttribute
on your attribute class. The indicated attribute class must derive from Attribute, either directly or indirectly.
GetCustomAttributes: This method gets all customer attributes for the assembly.
This example also demonstrates how EmployeeAttribute is converted to MyTest type.
Downloads
The sample code can be downloaded from below link. The code is complied with Microsoft Visual C# 2005.
|