麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

【C#】屬性(Attribute)

2019-11-17 02:57:44
字體:
來源:轉載
供稿:網友

【C#】屬性(Attribute)

如果程序員是貓,你是哪只貓?

這個是我一直都很喜歡的一個技術,不是很麻煩,也不是很難理解,和反射配合起來,只有你想不到沒有做不到的用途(夸張了哈)。

運用范圍

程序集,模塊,類型(類,結構,枚舉,接口,委托),字段,方法(含構造),方法,參數,方法返回值,屬性(PRoperty),Attribute

  

 [AttributeUsage(AttributeTargets.All)]    public class TestAttribute : Attribute    {    }    [TestAttribute]//結構    public struct TestStruct { }        [TestAttribute]//枚舉    public enum TestEnum { }    [TestAttribute]//類上    public class TestClass    {        [TestAttribute]        public TestClass() { }                [TestAttribute]//字段        private string _testField;        [TestAttribute]//屬性        public string TestProperty { get; set; }        [TestAttribute]//方法上        [return: TestAttribute]//定義返回值的寫法        public string TestMethod([TestAttribute] string testParam)//參數上        {            throw new NotImplementedException();        }    }
View Code

這里我們給出了除了程序集和模塊以外的常用的Atrribute的定義。

自定義Attribute

為了符合“公共語言規范(CLS)”的要求,所有的自定義的Attribute都必須繼承System.Attribute。

第一步:自定義一個檢查字符串長度的Attribute

    [AttributeUsage(AttributeTargets.Property)]    public class StringLengthAttribute : Attribute    {        private int _maximumLength;        public StringLengthAttribute(int maximumLength)        {            _maximumLength = maximumLength;        }        public int MaximumLength        {            get { return _maximumLength; }        }    }

AttributeUsage這個系統提供的一個Attribute,作用來限定自定義的Attribute作用域,這里我們只允許這個Attribute運用在Property上,內建一個帶參的構造器,讓外部傳入要求的最大長度。

第二步:創建一個實體類來運行我們自定義的屬性

 public class People    {        [StringLength(8)]        public string Name { get; set; }        [StringLength(15)]        public string Description { get; set; }    }

定義了兩個字符串字段Name和Description, Name要求最大長度為8個,Description要求最大長度為15.

第三步:創建驗證的類

 public class ValidationModel    {        public void Validate(object obj)        {            var t = obj.GetType();            //由于我們只在Property設置了Attibute,所以先獲取Property            var properties = t.GetProperties();            foreach (var property in properties)            {                //這里只做一個stringlength的驗證,這里如果要做很多驗證,需要好好設計一下,千萬不要用if elseif去鏈接                //會非常難于維護,類似這樣的開源項目很多,有興趣可以去看源碼。                if (!property.IsDefined(typeof(StringLengthAttribute), false)) continue;                var attributes = property.GetCustomAttributes();                foreach (var attribute in attributes)                {                    //這里的MaximumLength 最好用常量去做                    var maxinumLength = (int)attribute.GetType().                        GetProperty("MaximumLength").                        GetValue(attribute);                    //獲取屬性的值                    var propertyValue = property.GetValue(obj) as string;                    if (propertyValue == null)                        throw new Exception("exception info");//這里可以自定義,也可以用具體系統異常類                    if (propertyValue.Length > maxinumLength)                        throw new Exception(string.Format("屬性{0}的值{1}的長度超過了{2}", property.Name, propertyValue, maxinumLength));                }            }        }    }
View Code

這里用到了反射,因為Attribute一般都會和反射一起使用,這里驗證了字符串長度是否超過所要求的,如果超過了則會拋出異常

   private static void Main(string[] args)        {            var people = new People()            {                Name = "qweasdzxcasdqweasdzxc",                Description = "description"            };            try            {                new ValidationModel().Validate(people);            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            Console.ReadLine();        }
View Code

基礎篇不涉及很多高級用法,這個明白以后,變化可以很多。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品自在线拍 | 精品国产一区二区三区四区阿崩 | 国产噜噜噜噜久久久久久久久 | 久久久久久久久久美女 | 高清一区二区在线观看 | 国产高清一区 | 久久精品影视 | 男人久久天堂 | 亚洲精品欧美在线 | 国产人成精品一区二区三 | 91麻豆精品国产91久久久点播时间 | 性爱网站 | 久久激情小视频 | 成人富二代短视频 | 操操操操网 | 99国产精品欲a | 国产无遮挡一级毛片 | 国产精品视频免费网站 | 国产一区网址 | 黄色成人av在线 | 嗯哈~不行好大h双性 | 性欧美xxxx免费岛国不卡电影 | 欧美黄色片免费看 | 在线亚洲免费 | 久草在线视频网 | 欧美精品一级 | 一级免费毛片 | 在线播放av片 | www.热| 精品一区二区久久久久 | 日本在线播放一区二区三区 | 性少妇freeseⅹbbwhd | 成人免费看毛片 | 亚洲精品wwww| 在线播放免费人成毛片乱码 | 国产精品亚洲欧美一级在线 | 中文字幕在线永久视频 | 国产精品久久久久久久午夜片 | 亚久久 | 国产98色在线 | 国产精品久久久久久婷婷天堂 |