Demonstrate the Conditional attribute


   

// Demonstrate the Conditional attribute. 
 
#define TRIAL 
 
using System; 
using System.Diagnostics; 
 
public class TestAno { 
 
  [Conditional("TRIAL")]  
  void trial() { 
    Console.WriteLine("Trial version, not for distribution."); 
  } 
 
  [Conditional("RELEASE")]  
  void release() { 
    Console.WriteLine("Final release version."); 
  } 
 
  public static void Main() { 
    TestAno t = new TestAno(); 
 
    t.trial(); // call only if TRIAL is defined 
    t.release(); // called only if RELEASE is defined 
  } 
}


           
          


Use a property as a named attribute parameter


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use a property as a named attribute parameter. 
  
using System;  
using System.Reflection; 
  
[AttributeUsage(AttributeTargets.All)] 
class RemarkAttribute : Attribute { 
  string remarkValue; // underlies remark property 
 
  int pri_priority; // underlies priority property 
 
  public string supplement; // this is a named parameter 
 
  public RemarkAttribute(string comment) { 
    remarkValue = comment; 
    supplement = "None"; 
  } 
 
  public string remark { 
    get { 
      return remarkValue; 
    } 
  } 
 
  // Use a property as a named parameter. 
  public int priority { 
    get { 
      return pri_priority; 
    } 
    set { 
      pri_priority = value; 
    } 
  } 
}  
 
[RemarkAttribute("This class uses an attribute.", 
                 supplement = "This is additional info.", 
                 priority = 10)] 
class UseAttrib { 
  // ... 
} 
 
public class NamedParamDemo11 {  
  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); 
 
    Console.WriteLine("Priority: " + ra.priority); 
  }  
} 



           
          


Use AttributeUsage

   
 

using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct,Inherited = false)]
public class ClassVersionAttribute : System.Attribute {
    public ClassVersionAttribute(string target) : this(target, target) {
    }

    public ClassVersionAttribute(string target,string current) {
        m_TargetVersion = target;
        m_CurrentVersion = current;
    }

    private bool m_UseCurrentVersion = false;
    public bool UseCurrentVersion {
        set {
            if (m_TargetVersion != m_CurrentVersion) {
                m_UseCurrentVersion = value;
            }
        }
        get {
            return m_UseCurrentVersion;
        }
    }

    private string m_CurrentName;
    public string CurrentName {
        set {
            m_CurrentName = value;
        }
        get {
            return m_CurrentName;
        }
    }

    private string m_TargetVersion;
    public string TargetVersion {
        get {
            return m_TargetVersion;
        }
    }

    private string m_CurrentVersion;
    public string CurrentVersion {
        get {
            return m_CurrentVersion;
        }
    }
}

    


Defining New Attribute Classes

   
 


using System;
using System.Diagnostics;
using System.Reflection;
   
[AttributeUsage(AttributeTargets.Class)]
public class ClassAuthorAttribute : Attribute
{
    private string AuthorName;
   
    public ClassAuthorAttribute(string AuthorName)
    {
        this.AuthorName = AuthorName;
    }
   
    public string Author
    {
        get
        {
            return AuthorName;
        }
    }
}
   
[ClassAuthor("AA")]
public class TestClass
{
    public void Method1()
    {
        Console.WriteLine("Hello from Method1!");
    }
   
    [Conditional("DEBUG")]
    public void Method2()
    {
        Console.WriteLine("Hello from Method2!");
    }
   
    public void Method3()
    {
        Console.WriteLine("Hello from Method3!");
    }
   
}
   
public class MainClass
{
    public static void Main()
    {
        TestClass MyTestClass = new TestClass();
   
        MyTestClass.Method1();
        MyTestClass.Method2();
        MyTestClass.Method3();
   
        object []  ClassAttributes;
        MemberInfo TypeInformation;
   
        TypeInformation = typeof(TestClass);
        ClassAttributes = TypeInformation.GetCustomAttributes(typeof(ClassAuthorAttribute), false);
        if(ClassAttributes.GetLength(0) != 0)
        {
            ClassAuthorAttribute ClassAttribute;
   
            ClassAttribute = (ClassAuthorAttribute)(ClassAttributes[0]);
            Console.WriteLine("Class Author: {0}", ClassAttribute.Author);
        }
    }
}

    


Attribute in class inheritance

   
 
using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

public class Starter {
    public static void Main() {
        GenericIdentity g = new GenericIdentity("Person1");
        GenericPrincipal p = new GenericPrincipal(g,new string[] { "Manager" });
        Thread.CurrentPrincipal = p;
        MyClass.MethodA();
        YClass.MethodA();
    }
}

[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
public class MyClass {
    static public void MethodA() {
        Console.WriteLine("MyClass.MethodA");
    }
}

[PrincipalPermission(SecurityAction.Demand,Role = "Accountant")]
public class YClass : MyClass {

    static public void MethodB() {
        Console.WriteLine("MyClass.MethodB");
    }
}

    


Creating and using a class attribute.

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


   

// 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); 
  }  
}