输出类属性
using System; using System.Reflection;
namespace Attribute02 { //用于Class和Struct类型 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] class { private private private
public Creator(string name, string date) { this.name = name; this.date = date; this.version = 0.1; }
public { get { return set { this.version = value; } }
//输出 public { Console.WriteLine("Name: {0} Date: {1} Version: {2}", this.name, this.date, this.version); } } [Creator("PSM", "2013-09-01", Version=1.0)] class { public ATestClass01() { } } [Creator("PSM", "2013-09-01", Version = 2.0)] class { public ATestClass02() { } } [Creator("PSM", "2013-09-01", Version = 3.0)] class { public ATestClass03() { } }
class { static { PrintAttributeInformation(typeof(ATestClass01)); PrintAttributeInformation(typeof(ATestClass02)); PrintAttributeInformation(typeof(ATestClass03)); Console.ReadLine(); }
static { Console.WriteLine("Attributes for class {0}", typeName); object[] attr = typeName.GetCustomAttributes(true); foreach (object o in attr) { Console.WriteLine("Attribute {0}", 0); if (o is { ((Creator)o).Dump(); } } } } } |