ConsoleApplication--控制檯應用程序ide
首先建立基類:函數
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 { } }
其次自定義一個Attribute:this
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 { } }
在主入口中:spa
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(); } } }
先看看基類的效果:
3d
下面進入主題--如何在不改變基類及其繼承類的對象的狀況下,僅僅經過聲明Attribute,使得這些繼承類具備不一樣的行爲或者效果?code
1.聲明Attribute:在BasicClass.cs Line 23處增長 [Customized] ;對象
2.增長相應Attribute的方法:blog
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("+++++++++++++++++++++++++++++++++++++++++++++++++"); } } } }
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(); } } }
結果截圖:
ci
是否是感受很神奇呢? 並無對這個繼承類作任何修改,只是多了個聲明,而後附加相應的方法就實現了擁有不一樣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 { } }
方法調用:
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); } } } }
在繼承類前聲明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 { } }
結果:
注:未經做者贊成,禁止轉載!轉載請說明出處!請尊重知識產權。