首页 > 其他 > 详细

编写自定义特性

时间:2014-10-29 02:04:49      阅读:248      评论:0      收藏:0      [点我收藏+]

===========================Document.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    [AttributeUsage(AttributeTargets.Property,//该特性能够应用的范围(只能用于属性)
        AllowMultiple = true,//是否支持多个应用到同一个项上
        Inherited = true)//如果特性应用到类或接口上,就会自动应用到所有派生的类或接口上,
        ]              //如果特性应用在属性或方法上,就会自动应用到该方法或属性的重写版本上
    public class DocumentAttribute : Attribute
    {
        private string m_strName;
        private bool m_blnIsYes;
        public bool IsYes { get { return m_blnIsYes; } set { m_blnIsYes = value; } }
        public DocumentAttribute(string p_name)
        {
            this.m_strName = p_name;
        }
    }
}

===========================主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    class Program
    {
        //指定特性
        [Document("文档一",//构造函数需要传入的值
            IsYes = true)]//可选参数(Document类的属性)
        public string Name { get; set; }
        static void Main(string[] args)
        {
        }
    }
}

本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1569008

编写自定义特性

原文:http://962410314.blog.51cto.com/7563109/1569008

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!