zoukankan      html  css  js  c++  java
  • Attribute 特性

    特性可以给附加特性的对象附加元数据,附加一些信息,比如长度不能超过20(MaxLength=20),然后用反射获取数据(获取MaxLength)。

    (元数据记录了这个程序集里有多少个namespace、多少个类、类里有什么成员、成员的访问级别是什么……而且,元数据是以文本(也就是Unicode字符)形式存在的,使用.NET的反射(Reflection)技术,很容易就能把它们读取出来。一个程序集(.EXE.DLL)能够使用包含在自己体内的元数据来完整地说明自己,而不必像C/C++那样带着一大捆头文件,这就叫作“自包含性”或“自描述性”。)

    自定义特性

    namespace AttributeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                foreach(var pi in typeof(SysInfo).GetProperties())
                {
                    CustomAttribute att = Attribute.GetCustomAttribute(pi, typeof(CustomAttribute)) as CustomAttribute;
                    if (att != null)
                    {
                        Console.WriteLine(att.Description);
                        Console.WriteLine(att.Name);
                    }
                }
                Console.ReadKey();
            }
        }
        [AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
        public class CustomAttribute:Attribute
        {
            public string Name { get; set; }
            public string Description { get; set; }
            public CustomAttribute(string name)
            {
                Name = name;
            }
        }
        public class SysInfo
        {
            [Custom("呵呵",Description ="女神之蔑视")]
            public string Id
            {
                get;set;
            }
        }
    }

    原理:http://blog.csdn.net/FantasiaX/article/details/1636913

  • 相关阅读:
    首尾相连一维数组的最大子数组和
    二柱子——在线答题
    二维数组求最大值
    最大子数组和 10.11作业
    最大子数组

    异常
    面向对象2
    面向对象1
    java数据类型
  • 原文地址:https://www.cnblogs.com/shuangzimuchangzhu/p/8550659.html
Copyright © 2011-2022 走看看