日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过声明Attribute属性改变不同类的输出效果

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过声明Attribute属性改变不同类的输出效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ConsoleApplication--控制臺應用程序

首先創建基類:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {/// <summary>/// 此處的注釋不影響編譯和運行,只是給開發者提供幫助/// </summary> //[some Attribute] e.g.[Obsolete] //[Obsolete]、[Obsolete("")]、[Obsolete("",true/false)] Obsolete的構造函數 F12查看 [Serializable]public class BasicClass{//基類屬性、字段public int ID { set; get; }public string Name { set; get; }}public class AClass:BasicClass{}public class BClass : BasicClass{ }public class CClass : BasicClass{ } } View Code

其次自定義一個Attribute:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {/// <summary>/// 自定義一個Attribute:somename+Attribute/// 使用時:[somename]/// </summary>public class CustomizedAttribute:Attribute{//以下是構造函數public CustomizedAttribute(){ // }public CustomizedAttribute(string msg){ }public CustomizedAttribute(string msg, bool err){ }//字段public string Msg;public bool Err;}public class esleCusomizedAttribute : Attribute{ } } View Code

在主入口中:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {class Program{static void Main(string[] args){//主要運用了繼承的機制BasicClass bc = new BasicClass() { ID=1,Name="基類1"};Console.WriteLine("基類ID:{0} ;基類類名:{1}",bc.ID,bc.Name);Console.ReadLine();}} } View Code

先看看基類的效果:

?

下面進入主題--如何在不改變基類及其繼承類的對象的情況下,僅僅通過聲明Attribute,使得這些繼承類具有不同的行為或者效果?

1.聲明Attribute:在BasicClass.cs Line 23處增加 [Customized] ;

2.增加相應Attribute的方法:

using System; using System.Collections.Generic; using System.Linq; using System.Reflection;//反射 using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {public class AttributeActivities{public static void OutPut<T>(T t) where T : BasicClass{Type type=t.GetType();Attribute attribute = type.GetCustomAttribute(typeof(CustomizedAttribute));//注意 using System.Reflection;if (attribute != null && attribute is CustomizedAttribute){CustomizedAttribute customizedAttr = attribute as CustomizedAttribute;Console.WriteLine("當前類額外的Attribute:{0}",customizedAttr.ToString().Substring(customizedAttr.ToString().IndexOf(".")+1));Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++");}}} } View Code

3.在程序主入口調用方法:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {class Program{static void Main(string[] args){//主要運用了繼承的機制BasicClass bc = new BasicClass() { ID=1,Name="基類1"};Console.WriteLine("基類ID:{0} ;基類類名:{1}",bc.ID,bc.Name);Console.WriteLine("*************************************************");AttributeActivities.OutPut<AClass>(new AClass(){ID=2,Name="AClass" });Console.WriteLine("*************************************************");AttributeActivities.OutPut<BClass>(new BClass(){ID = 3,Name = "BClass"});Console.WriteLine("*************************************************");AttributeActivities.OutPut<CClass>(new CClass(){ID = 4,Name = "CClass"});Console.ReadLine();}} } View Code

結果截圖:

?

是不是感覺很神奇呢? 并沒有對這個繼承類做任何修改,只是多了個聲明,然后附加相應的方法就實現了擁有不同Attribute的類具有不同的效果。

讀者也可以自己擴展其它的方法試試哦。。。

補充:

Attribute里增加:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {/// <summary>/// 自定義一個Attribute:somename+Attribute/// 使用時:[somename]/// </summary>public class CustomizedAttribute:Attribute{//以下是構造函數public CustomizedAttribute(){ // }public CustomizedAttribute(string msg){}public CustomizedAttribute(string msg, bool err){ }//字段public string Msg;public bool Err;//增加部分//擴展方法public int Number;public void DoSomeThing(string msg){if (Err){Console.WriteLine("{0}--{1}",msg,DateTime.Now);}}public CustomizedAttribute(string msg, int number, bool err) {this.Msg = msg;this.Number = number;this.Err = err;}}public class esleCusomizedAttribute : Attribute{ } } View Code

方法調用:

using System; using System.Collections.Generic; using System.Linq; using System.Reflection;//反射 using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {public class AttributeActivities{public static void OutPut<T>(T t) where T : BasicClass{Type type=t.GetType();Attribute attribute = type.GetCustomAttribute(typeof(CustomizedAttribute));//注意 using System.Reflection;if (attribute != null && attribute is CustomizedAttribute){CustomizedAttribute customizedAttr = attribute as CustomizedAttribute;Console.WriteLine("當前類額外的Attribute:{0}",customizedAttr.ToString().Substring(customizedAttr.ToString().IndexOf(".")+1));Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++");//調用擴展方法 customizedAttr.DoSomeThing(t.Name);}}} } View Code

在繼承類前聲明Attribute:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Attribute_Exercise {/// <summary>/// 此處的注釋不影響編譯和運行,只是給開發者提供幫助/// </summary> //[some Attribute] e.g.[Obsolete] //[Obsolete]、[Obsolete("")]、[Obsolete("",true/false)] Obsolete的構造函數 F12查看 [Serializable] public class BasicClass{//基類屬性、字段public int ID { set; get; }public string Name { set; get; }}//聲明Attribute [Customized] public class AClass:BasicClass{}//聲明擴展Attribute的構造函數[Customized("",1,true)]public class BClass : BasicClass{ }public class CClass : BasicClass{ } } View Code

結果:

注:未經作者同意,禁止轉載!轉載請說明出處!請尊重知識產權。

?

轉載于:https://www.cnblogs.com/5696-an/p/5638312.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的通过声明Attribute属性改变不同类的输出效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美女人天堂 | 成人片在线免费看 | 日韩avv | 激情综合网激情 | 中文字幕 国产精品 | 久草热播| 日韩视频精品 | 99久久久无码国产精品免费 | 777久久久精品一区二区三区 | 成人免费va视频 | 视频在线日韩 | 亚洲国产一区二区三区四区 | 色射影院 | 欧美成人精品一区二区三区在线看 | 狠狠艹狠狠干 | caoporn视频在线| 国产精品老牛影视 | 日韩精品免费在线 | 深夜激情网站 | 激情综合视频 | 欧美激情在线观看一区 | 好吊一区二区三区视频 | 欧美区日韩区 | 大波大乳videos巨大 | 国产五区| 亚洲欧美日韩国产一区二区三区 | 黄色一集片 | 天天色影网 | 99精品在线观看视频 | 亚洲一级视频在线观看 | 久久午夜剧场 | 黑丝av在线| 日本理论片午伦夜理片在线观看 | 国产中文在线 | 狗爬女子的视频 | 欧美女优在线观看 | 亚洲av无码国产精品久久 | 亚洲欧美自拍另类 | 国产一二三区av | 亚洲精品国产精品国自 | 欧美激情小视频 | 亚洲成年网站 | 强睡邻居人妻中文字幕 | 高h视频在线播放 | 久久国产精品久久 | 亚洲欧美精品午睡沙发 | 国产中文字幕在线视频 | 一区二区三区福利 | 欧美一卡二卡 | 国内自拍偷拍 | 亚洲av无码专区在线播放中文 | 在线观看高清av | 老公吃小头头视频免费观看 | 国产精品熟妇一区二区三区四区 | 亚洲久久色 | 偷偷操av| 亚洲av无码专区国产乱码不卡 | 人妻人人澡人人添人人爽 | 亚洲av成人无码久久精品老人 | 一区二区三区久久久 | 亚洲精品20p| 丝袜视频一区 | 国产精品高清在线 | 极品美女被c | 亚洲视频不卡 | 一区二区三区毛片 | 日批视频在线免费看 | 亚洲国产精品激情在线观看 | 久久久久久久久网站 | 一级片免费在线 | 国产精品日韩一区二区三区 | 国产精品区二区三区日本 | 亚洲熟妇av乱码在线观看 | 中文字幕精品三级久久久 | 67194国产 | 国产在线三区 | 黄色一级一片免费播放 | 成年人性生活视频 | 天堂视频免费 | 极品美女无套呻吟啪啪 | 波多野结衣欲乱上班族 | 天天艹av | 青青草国产在线播放 | 亚洲色欧美 | 手机av网站| 噼里啪啦国语版在线观看 | 卡通动漫亚洲综合 | 日韩三级免费观看 | 国产一区二区观看 | 日本特级片 | 伦理亚洲 | 久久久久久久国产视频 | 中文字幕欲求不满 | 麻豆剧场| 国产三级三级看三级 | 自拍偷拍免费 | 久久久久久久久久久久久国产 | 激情五月婷婷久久 | 51久久|