编写自定义特性

发布时间:2020-06-26 13:52:43 作者:1473348968
来源:网络 阅读:469

===========================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)
        {
        }
    }
}

推荐阅读:
  1. 编写可复用的自定义按钮
  2. 自定义特性用途---案例:操作权限

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

编写自定义特性

上一篇:Python中有关树的使用方法

下一篇:Python中的代码块和非代码块是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》