Creating and using a class attribute.

image_pdfimage_print

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class Creator : System.Attribute {
public Creator(string name, string date) {
this.name = name;
this.date = date;
version = 0.1;
}
string date;
string name;
public double version;

}

[Creator(“T”, “05/01/2001”, version = 1.1)]
class MainClass {
static public void Main(String[] args) {
for (int i = 0; i < args.Length; ++i) System.Console.WriteLine("Args[{0}] = {1}", i, args[i]); } } [/csharp]

Use a named attribute parameter

image_pdfimage_print

   

// Use a named attribute parameter. 
  
using System;  
using System.Reflection; 
  
[AttributeUsage(AttributeTargets.All)] 
class RemarkAttribute : Attribute { 
  string remarkValue; // underlies remark property 
 
  public string supplement; // this is a named parameter 
 
  public RemarkAttribute(string comment) { 
    remarkValue = comment; 
    supplement = "None"; 
  } 
 
  public string remark { 
    get { 
      return remarkValue; 
    } 
  } 
}  
 
[RemarkAttribute("This class uses an attribute.", 
                 supplement = "This is additional info.")] 
class UseAttrib { 
  // ... 
} 
 
public class NamedParamDemo {  
  public static void Main() {  
    Type t = typeof(UseAttrib); 
 
    Console.Write("Attributes in " + t.Name + ": "); 
 
    object[] attribs = t.GetCustomAttributes(false);  
    foreach(object o in attribs) { 
      Console.WriteLine(o); 
    } 
 
    // Retrieve the RemarkAttribute. 
    Type tRemAtt = typeof(RemarkAttribute); 
    RemarkAttribute ra = (RemarkAttribute) 
          Attribute.GetCustomAttribute(t, tRemAtt); 
 
    Console.Write("Remark: "); 
    Console.WriteLine(ra.remark); 
 
    Console.Write("Supplement: "); 
    Console.WriteLine(ra.supplement); 
  }  
} 



           
          


A simple attribute example

image_pdfimage_print

   

using System;  
using System.Reflection; 
  
[AttributeUsage(AttributeTargets.All)] 
class RemarkAttribute : Attribute { 
  string remarkValue; 
 
  public RemarkAttribute(string comment) { 
    remarkValue = comment; 
  } 
 
  public string remark { 
    get { 
      return remarkValue; 
    } 
  } 
}  
 
[RemarkAttribute("This class uses an attribute.")] 
class UseAttrib { 
  // ... 
} 
 
public class AttribDemo {  
  public static void Main() {  
    Type t = typeof(UseAttrib); 
 
    Console.Write("Attributes in " + t.Name + ": "); 
 
    object[] attribs = t.GetCustomAttributes(false);  
    foreach(object o in attribs) { 
      Console.WriteLine(o); 
    } 
 
    Console.Write("Remark: "); 
 
    // Retrieve the RemarkAttribute. 
    Type tRemAtt = typeof(RemarkAttribute); 
    RemarkAttribute ra = (RemarkAttribute) 
          Attribute.GetCustomAttribute(t, tRemAtt); 
 
 
    Console.WriteLine(ra.remark); 
  }  
}


           
          


Subclass System.Attribute

image_pdfimage_print
   
 
using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class AuthorAttribute : System.Attribute
{
    private string company; 
    private string name;

    public AuthorAttribute(string name)
    {
        this.name = name;
        company = "";
    }

    public string Company
    {
        get { return company; }
        set { company = value; }
    }

    public string Name
    {
        get { return name; }
    }
}


[assembly: Author("Tom", Company = "Ltd.")]
[Author("Tom", Company = "Abc Ltd.")]
class SomeClass { }

[Author("Lena")]
public class SomeOtherClass
{
}


[Author("FirstName")]
[Author("Jack", Company = "Ltd.")]
class MainClass
{
    public static void Main()
    {
        Type type = typeof(MainClass);

        object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true);

        foreach (AuthorAttribute a in attrs)
        {
            Console.WriteLine(a.Name + ", " + a.Company);
        }
    }
}