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

    }

    }

    }

    }

    }

  • 相关阅读:
    LeetCode_637.二叉树的层平均值
    LeetCode_627.变更性别
    LeetCode_617.合并二叉树
    LeetCode_595.大的国家
    LeetCode_590.N叉树的后序遍历
    LeetCode_589.N叉树的前序遍历
    LeetCode_58.最后一个单词的长度
    LeetCode_566.重塑矩阵
    LeetCode_561.数组拆分 I
    LeetCode_56.合并区间
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3305866.html
Copyright © 2011-2022 走看看