A simple attribute example


   

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

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

    


Using the as Keyword to Work with an Interface

   
 


using System;
   
public interface IPrintMessage
{
    void Print();
};
   
class Class1
{
    public void Print()
    {
        Console.WriteLine("Hello from Class1!");
    }
}
   
class Class2 : IPrintMessage
{
    public void Print()
    {
        Console.WriteLine("Hello from Class2!");
    }
}
   
class MainClass
{
    public static void Main()
    {
        PrintClass   PrintObject = new PrintClass();
   
        PrintObject.PrintMessages();
    }
}
   
class PrintClass
{
    public void PrintMessages()
    {
        Class1      Object1 = new Class1();
        Class2      Object2 = new Class2();
   
        PrintMessageFromObject(Object1);
        PrintMessageFromObject(Object2);
    }
   
    private void PrintMessageFromObject(object obj)
    {
        IPrintMessage PrintMessage;
   
        PrintMessage = obj as IPrintMessage;
        if(PrintMessage != null)
            PrintMessage.Print();
    }
}

    


as operator

   
 


using System;
   
class Employee { }
   
class ContractEmployee : Employee { }
   
class CastExample5
{
    public static void Main ()
    {
        Employee e = new Employee();
        Console.WriteLine("e = {0}", e == null ? "null" : e.ToString());
   
        ContractEmployee c  = e as ContractEmployee;
        Console.WriteLine("c = {0}", c == null ? "null" : c.ToString());
    }
}