码迷,mamicode.com
首页 > 其他好文 > 详细

定制特性

时间:2017-06-18 15:06:38      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:multiple   set   err   att   protect   log   引用   显示   ted   

1,定制特性

①C#允许用一个前缀明确指定特性要应用的目标元素

[assembly: AssemblyVersion("1.0.0.0")]

②AttributeUsage. Inherited(特性应用于基类时,是否同时应用于派生类和重写的方法)

    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,Inherited = true)]
    internal class TastyAttribute : Attribute
    {
    }

    [Tasty,Serializable]
    internal class BaseType
    {
        [Tasty]
        protected virtual void DoSomething(){}
    }
    //继承父类Tasty的特性
    internal class DerivedType : BaseType
    {
        //继承Tasty的特性
        protected override void DoSomething(){}
    }
    //如果特性类没有显示的引用AttributeUsage,则默认AttributeUsage如下
    [AttributeUsage(AttributeTargets.All,AllowMultiple = false, Inherited = true)]

 

定制特性

标签:multiple   set   err   att   protect   log   引用   显示   ted   

原文地址:http://www.cnblogs.com/zd1994/p/7044203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!