溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

編寫自定義特性

發(fā)布時間:2020-06-26 13:52:43 來源:網(wǎng)絡(luò) 閱讀:469 作者:1473348968 欄目:編程語言

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    [AttributeUsage(AttributeTargets.Property,//該特性能夠應(yīng)用的范圍(只能用于屬性)
        AllowMultiple = true,//是否支持多個應(yīng)用到同一個項上
        Inherited = true)//如果特性應(yīng)用到類或接口上,就會自動應(yīng)用到所有派生的類或接口上,
        ]              //如果特性應(yīng)用在屬性或方法上,就會自動應(yīng)用到該方法或?qū)傩缘闹貙懓姹旧?    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("文檔一",//構(gòu)造函數(shù)需要傳入的值
            IsYes = true)]//可選參數(shù)(Document類的屬性)
        public string Name { get; set; }
        static void Main(string[] args)
        {
        }
    }
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI