通过声明Attribute属性改变不同类的输出效果
生活随笔
收集整理的這篇文章主要介紹了
通过声明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 Code3.在程序主入口調用方法:
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属性改变不同类的输出效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何预约冬奥会纪念钞
- 下一篇: 扫描项目里没有使用的图片mac工具,删除