|
Answer» The primary steps to properly design custom ATTRIBUTE classes are as follows:
Applying the AttributeUsageAttribute
([AttributeUsage(AttributeTargets.All,
Inherited = false, AllowMultiple = true)])
Declaring the attribute. (class public class MyAttribute : System.Attribute { // .
. . })
Declaring constructors (public MyAttribute(bool myvalue) { this.myvalue =
myvalue; })
Declaring PROPERTIES
public bool MyProperty
{
get {return this.myvalue;}
set {this.myvalue = value;}
}
The following EXAMPLE demonstrates the basic way of using reflection to get access to custom attributes.
class MainClass
{
public static VOID Main()
{
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes();
for (int i = 0; i < attributes.Length; i ++)
{
System.Console.WriteLine(attributes[i]);
}
}
} The primary steps to properly design custom attribute classes are as follows:
Applying the AttributeUsageAttribute
([AttributeUsage(AttributeTargets.All,
Inherited = false, AllowMultiple = true)])
Declaring the attribute. (class public class MyAttribute : System.Attribute { // .
. . })
Declaring constructors (public MyAttribute(bool myvalue) { this.myvalue =
myvalue; })
Declaring properties
public bool MyProperty
{
get {return this.myvalue;}
set {this.myvalue = value;}
} The following example demonstrates the basic way of using reflection to get access to custom attributes.
class MainClass
{
public static void Main()
{
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes();
for (int i = 0; i < attributes.Length; i ++)
{
System.Console.WriteLine(attributes[i]);
}
}
}
|