zoukankan      html  css  js  c++  java
  • C#的Attribute

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    
    namespace codeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //通过反射来获取Attribute中的信息
                MyAttribute myattribute;
                foreach (var attr in typeof(MyClass).GetCustomAttributes(true))
                {
                    myattribute = attr as MyAttribute;
                    Console.WriteLine(myattribute.Name);
                }
    
                Console.ReadLine();
            }
        }
    
        //自定义Attribute经常用到 AttributeUsage ,可以限制自定义Attribute的使用范围
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
        class MyAttribute : Attribute
        {
            //MyAttribute必须继承Attribute
            string name;
    
            public MyAttribute(string nameIn)
            {
                this.name = nameIn;
            }
    
            public string Name
            {
                get
                {
                    return this.name;
                }
            }
    
            public string desc
            {
                get;
                set;
            }
        }
    
        //MyAttribute可以缩写为  My
        [My("MyAttribute",desc="MyClass")]
        class MyClass
        {
    
        }
    
    }
  • 相关阅读:
    7、python数据类型之集合set
    python基本数据类型练习
    matplotlib
    numpy常用函数
    pillow包
    keras-tensorflow版本对应
    python-激活和切换运行环境
    端口监控
    numpy
    低风险创业笔记
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4781231.html
Copyright © 2011-2022 走看看