zoukankan      html  css  js  c++  java
  • Attribute

    一、  简介:

     class Person   
        {
            [ReadOnly(true)] //只读
            [DisplayName("姓名")] //显示成中文
            [Browsable(false)] //不显示
            public int Age { get; set; }
            public string Name { get; set; }
    
            [Obsolete]  //标记成员已过时
            public void SayHi()
            {
                Console.WriteLine("大家好:我是{0},年龄{1}", Name, Age);
            }
        }

    二、判断是否使用attribute及自定义:

       class Person   
        {
            [ReadOnly(true)] //只读
            [DisplayName("姓名")] //显示成中文
            [Browsable(false)] //不显示
            public int Age { get; set; }
            public string Name { get; set; }
    
            [Obsolete]
            public void SayHi()
            {
                Console.WriteLine("大家好:我是{0},年龄{1}", Name, Age);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Person p1 = new Person();
                p1.Name = "chen";
                p1.Age = 22;
                Type type = p1.GetType();
    
                //查看标签是否有用
                object[] obsoltesAttrs = type.GetCustomAttributes(typeof(ObsoleteAttribute), false);
                if(obsoltesAttrs.Length>0)
                {
                    Console.WriteLine("这个类已经过时");
                }
                else
                {
                    Console.WriteLine("这个类没有过时");
                }
    
                //遍历属性
                foreach (PropertyInfo prop in type.GetProperties())
                {
                    string displayName;
                    //方式一
                    DisplayNameAttribute display=(DisplayNameAttribute)prop.GetCustomAttribute(typeof(DisplayNameAttribute));
                    if(display==null)
                    {
                        displayName = null;
                    }
                    else
                    {
                        displayName = display.DisplayName;
                    }
    
                    //方式二
                    /*
                    object[] diaplayAttrs = prop.GetCustomAttributes(typeof(DisplayNameAttribute), false);
                    if(diaplayAttrs.Length<=0)
                    {
                        displayName = null;
                    }
                    else
                    {
                        DisplayNameAttribute displayAttr = (DisplayNameAttribute)diaplayAttrs[0];
                        displayName = displayAttr.DisplayName;
                    }
                    */
                    string name = prop.Name;
                    object value = prop.GetValue(p1);
                    Console.WriteLine(name +"("+ displayName + ")"+ "=" + value);
                   // DisplayNameAttribute displayAttr = (DisplayNameAttribute)prop
                }
                Console.ReadKey();
            } 
            
        }
    //自定义attribute
        public class JanpanDisplayAttribute : Attribute
        {
    
            public string DisplayName { get; set; }
            public JanpanDisplayAttribute(string name)
            {
                this.DisplayName = name;
            }
        }
       
  • 相关阅读:
    phome_enewsclass 数据表字段解释(栏目主表)
    phome_ecms_news 数据表字段解释(新闻系统模型-主表)
    帝国cms7.2灵动标签万能教程
    帝国cms7.2自定义列表建立tag效果 代码 教程
    栏目自定义变量怎么用?
    给你的网站404页面加上“宝贝寻亲”公益页面
    discuz_style_default.xml修改
    谈谈几大宗教.
    discuz默认模板文件结构详解-模板文件夹介绍
    一些适合用手柄玩的网络游戏
  • 原文地址:https://www.cnblogs.com/fuyouchen/p/9361404.html
Copyright © 2011-2022 走看看