Csharp/C Sharp/Reflection/Attributes

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Attributes:Reflecting on Attributes

using System;
using System.Reflection;
public class AttributesReflectingonAttributes
{
    public static void Main()
    {
        Type type = typeof(Complex);
        foreach (CodeReviewAttribute att in
        type.GetCustomAttributes(typeof(CodeReviewAttribute), false))
        {
            Console.WriteLine("Reviewer: {0}", att.Reviewer);
            Console.WriteLine("Date: {0}", att.Date);
            Console.WriteLine("Comment: {0}", att.rument);
        }
    }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]
public class CodeReviewAttribute: System.Attribute
{
    public CodeReviewAttribute(string reviewer, string date)
    {
        this.reviewer = reviewer;
        this.date = date;
    }
    public string Comment
    {
        get
        {
            return(comment);
        }
        set
        {
            comment = value;
        }
    }
    public string Date
    {
        get
        {
            return(date);
        }
    }
    public string Reviewer
    {
        get
        {
            return(reviewer);
        }
    }
    string reviewer;
    string date;
    string comment;
}

[CodeReview("AA", "01-12-2000", Comment="Joe" Code")]
[CodeReview("BB", "01-01-2000", Comment="Revisit this section")]
class Complex
{
}


Displaying attributes for a class.

 

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;
    }
    public void Dump() {
        Console.WriteLine("Name {0}", name);
        Console.WriteLine("Date {0}", date);
        Console.WriteLine("Version {0}", version);
    }
    string date;
    string name;
    public double version;
}
[Creator("A", "01/01/2008", version = 1.1)]
class ATestClass1 {
    ATestClass1() {
    }
}
[Creator("B", "04/01/2008", version = 1.5)]
class ATestClass2 {
    ATestClass2() {
    }
}
[Creator("C", "12/31/2008", version = 2.5)]
class ATestClass3 {
    ATestClass3() {
    }
}

class MainClass {
    public static void PrintAttributeInformation(Type classType) {
        Console.WriteLine("Attributes for class {0}", classType);
        object[] attr = classType.GetCustomAttributes(true);
        foreach (object o in attr) {
            Console.WriteLine("Attribute {0}", o);
            if (o is Creator)
                ((Creator)o).Dump();
        }
    }
    public static void Main() {
        PrintAttributeInformation(typeof(ATestClass1));
        PrintAttributeInformation(typeof(ATestClass2));
        PrintAttributeInformation(typeof(ATestClass3));
    }
}


Use GetCustomAttribute

 
using System;
using System.Reflection;

class Starter {
    static void Main() {
        Type tObj = typeof(Starter);
        MethodInfo method = tObj.GetMethod("AMethod");
        Attribute attrib = Attribute.GetCustomAttribute(method, typeof(ObsoleteAttribute));
        ObsoleteAttribute obsolete = (ObsoleteAttribute)attrib;
        Console.WriteLine("Obsolete Message: " + obsolete.Message);
    }
    [Obsolete("Deprecated function.", false)]
    public void AMethod() {
    }
}