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 { } }
其次自定义一个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 { } }
在主入口中:
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(); } } }
先看看基类的效果:
下面进入主题--如何在不改变基类及其继承类的对象的情况下,仅仅通过声明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("+++++++++++++++++++++++++++++++++++++++++++++++++"); } } } }
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(); } } }
结果截图:
是不是感觉很神奇呢? 并没有对这个继承类做任何修改,只是多了个声明,然后附加相应的方法就实现了拥有不同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 { } }
结果:
注:未经作者同意,禁止转载!转载请说明出处!请尊重知识产权。