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=,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(".")+));
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=,Name="基类1"};
Console.WriteLine("基类ID:{0} ;基类类名:{1}",bc.ID,bc.Name);
Console.WriteLine("*************************************************");
AttributeActivities.OutPut<AClass>(new AClass()
{
ID=,
Name="AClass"
});
Console.WriteLine("*************************************************");
AttributeActivities.OutPut<BClass>(new BClass()
{
ID = ,
Name = "BClass"
});
Console.WriteLine("*************************************************");
AttributeActivities.OutPut<CClass>(new CClass()
{
ID = ,
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(".")+));
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("",,true)]
public class BClass : BasicClass
{ } public class CClass : BasicClass
{ }
}

结果:

注:未经作者同意,禁止转载!转载请说明出处!请尊重知识产权。

通过声明Attribute属性改变不同类的输出效果的更多相关文章

  1. 【ASP.NET】DataContract序列化,反序列化对象中包含用接口声明的属性时的处理方法

    为此对象添加KnownType属性(Attribute).类型为用接口声明的属性(Property)的所有可能类型.  示例如下: public interface IKey { [DataMembe ...

  2. iOS 使用COPY声明NSSTRING属性

    使用COPY声明NSSTRING属性 2014/05/29 JACE 发表回复 声明一个NSString属性使用copy要优于使用strong.这同样适用于遵守NSCoding协议的不可变类(immu ...

  3. WPF——数据绑定及属性改变事件

    一.首先需要封装一下文本框的属性,并且在实体类中添加一个实体类的属性改变函数 public class User : INotifyPropertyChanged //INotifyPropertyC ...

  4. python类内init外声明的属性与init内声明的对象属性的访问和操作区别

    python类内init外声明的属性与init内声明的对象属性的访问和操作区别(面试题) 1.在ipython中输入以下代码,其输出会是什么? In [1]: class ClassOut: ...: ...

  5. PHP使用static关键字声明静态属性和静态方法

    PHP使用static关键字声明静态属性和静态方法 在PHP中,通过static关键字修饰的成员属性和成员方法被称为静态属性和静态方法. 静态属性和静态方法不需要在被类实例化的情况下就可以直接使用. ...

  6. 借助JavaScript中的Dom属性改变Html中Table边框的颜色

    借助JavaScript中的Dom属性改变Html中Table边框的颜色 -------------------- <html> <head> <title>我是页 ...

  7. C#里Attribute属性

    系统内置属性 系统内置的Attribute属性Obsolete,被个这属性标记的方法在别的地方被调用的时候会有警告提示; 这个属性还可以指定第二个布尔参数,设置编译时是否报错; 例: [Obsolet ...

  8. OC中在.h和.m中声明的属性和成员变量有何区别?

    相比Swift而言,OC规矩太多. 差不多,.h中声明的属性和成员变量均可以在子类中访问到.而.m则不可.而属性其实也就是成员变量的一种简写,其内部自动包含了getter和setter方法. 如图:V ...

  9. silverlight属性改变事件通知

    工作中遇到silverlight本身没有提供的某些属性改变事件,但又需要在属性改变时得到通知,Google搬运stack overflow,原地址 /// Listen for change of t ...

随机推荐

  1. API -- java.lang.Integer

    java.lang Class Integer static Integer valueOf(int i) Returns an Integer instance representing the s ...

  2. MySQL 警告WARN: Establishing SSL connection without server's identity verification is not recommended.解决办法

    Fri Jun 17 13:46:54 CST 2016 WARN: Establishing SSL connection without server's identity verificatio ...

  3. VMware8.0虚拟机中安装Ubuntu12.04使用NAT设置连接网络

    之前一直尝试使用“桥接”的方法,但是一打开虚拟机,本机windows就断网.最后不得不换种方法,还好尝试了很多遍终于使用NAT设置成功的联网了. 说明:本机windows连接的是无线网. 1.检查自己 ...

  4. 点击按钮div显示,点击div或者document,div隐藏

    $("button").click(function(event){ event.stopPropagation(); if($("div").is(':hid ...

  5. (44) odoo中的WebService

    * 前言   erp系统会和其它系统进行对接,这时就要接口,官方给出的是两解决方案   * XML-RPCLibrary  举例    import xmlrpclib root = 'http:// ...

  6. JavaScript中concat()方法

    concat(参数) 用于数组之间的连接, arrayObject.concat(arrayX,arrayX,......,arrayX) 如: var a = [1,2,3]; document.w ...

  7. 版本控制简介,git使用----使用GitHub托管代码

    关于版本控制: 很久以前,人们苦于对写过的代码进行版本的管理,经常过了一段时间想恢复原来写过的代码却又忘了不知道丢到哪儿去了,有的人用加上时间后缀来命名文件的方法,便于后期维护,但是这样做的麻烦也很大 ...

  8. 点击空白处div消失的方法

    这是做的js页面的一部分,也是上一篇文章中加载json格式数据后展示的效果界面. 现在的问题是:点击南京市后会弹出下面的白色的框,点击框右上角的X号后会关闭白色的框,现在想点击白色的框周围的任一地方, ...

  9. python 学习2

    在1的基础上 settings.py blog/models.py: 打开一个cmd, net start mysql ,启动mysql服务 mysql -uroot -p 再在py_fir目录下打开 ...

  10. 网站优化之PHPCMS如何开启伪静态

    做为一名网站优化方面的工作,那么选择CMS系统的时候,有良好的网站优化功能就是一个好的CMS的标准之一,而系统是否支持伪静态,则是URL优化的工作之一,而PHPCMS是一款网站优化方面做得比较成功的C ...