Displaying attributes for a class. : Attributes « Reflection « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Reflection » AttributesScreenshots 
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));
    }
}

 
Related examples in the same category
1.Use GetCustomAttribute
2.Attributes:Reflecting on AttributesAttributes:Reflecting on Attributes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.