zoukankan      html  css  js  c++  java
  • Attributes(2): Displaying attributes for a class.(显示类属性)

    输出类属性

     

    using System;

    using System.Reflection;

     

    namespace Attribute02

    {

    //用于Class和Struct类型

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]

    class Creator : Attribute

    {

    private string date;

    private string name;

    private double version;

     

    public Creator(string name, string date)

    {

    this.name = name;

    this.date = date;

    this.version = 0.1;

    }

     

    public Double Version

    {

    get { return this.version; }

    set { this.version = value; }

    }

     

    //输出

    public void Dump()

    {

    Console.WriteLine("Name: {0} Date: {1} Version: {2}", this.name, this.date, this.version);

    }

    }

    [Creator("PSM", "2013-09-01", Version=1.0)]

    class ATestClass01

    {

    public ATestClass01()

    {

    }

    }

    [Creator("PSM", "2013-09-01", Version = 2.0)]

    class ATestClass02

    {

    public ATestClass02()

    {

    }

    }

    [Creator("PSM", "2013-09-01", Version = 3.0)]

    class ATestClass03

    {

    public ATestClass03()

    {

    }

    }

     

    class Program

    {

    static void Main(string[] args)

    {

    PrintAttributeInformation(typeof(ATestClass01));

    PrintAttributeInformation(typeof(ATestClass02));

    PrintAttributeInformation(typeof(ATestClass03));

    Console.ReadLine();

    }

     

    static void PrintAttributeInformation(Type typeName)

    {

    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)

    {

    ((Creator)o).Dump();

    }

    }

    }

    }

    }

  • 相关阅读:
    mysql-master-ha 实现mysql master的高可用。
    一个不错的工具版本管理工具
    java的日志知识
    从解决一个java.lang.NoSuchMethodError想到的
    一个单点登录问题的解决
    关于2013年1月21日的DNS故障分析文章
    每日好的资源整理
    mongodb3.4 sharding安装文档
    python 函数
    codis3安装测试
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3305866.html
Copyright © 2011-2022 走看看