通过声明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
{ }
}
其次自定义一个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属性改变不同类的输出效果的更多相关文章
- 【ASP.NET】DataContract序列化,反序列化对象中包含用接口声明的属性时的处理方法
为此对象添加KnownType属性(Attribute).类型为用接口声明的属性(Property)的所有可能类型. 示例如下: public interface IKey { [DataMembe ...
- iOS 使用COPY声明NSSTRING属性
使用COPY声明NSSTRING属性 2014/05/29 JACE 发表回复 声明一个NSString属性使用copy要优于使用strong.这同样适用于遵守NSCoding协议的不可变类(immu ...
- WPF——数据绑定及属性改变事件
一.首先需要封装一下文本框的属性,并且在实体类中添加一个实体类的属性改变函数 public class User : INotifyPropertyChanged //INotifyPropertyC ...
- python类内init外声明的属性与init内声明的对象属性的访问和操作区别
python类内init外声明的属性与init内声明的对象属性的访问和操作区别(面试题) 1.在ipython中输入以下代码,其输出会是什么? In [1]: class ClassOut: ...: ...
- PHP使用static关键字声明静态属性和静态方法
PHP使用static关键字声明静态属性和静态方法 在PHP中,通过static关键字修饰的成员属性和成员方法被称为静态属性和静态方法. 静态属性和静态方法不需要在被类实例化的情况下就可以直接使用. ...
- 借助JavaScript中的Dom属性改变Html中Table边框的颜色
借助JavaScript中的Dom属性改变Html中Table边框的颜色 -------------------- <html> <head> <title>我是页 ...
- C#里Attribute属性
系统内置属性 系统内置的Attribute属性Obsolete,被个这属性标记的方法在别的地方被调用的时候会有警告提示; 这个属性还可以指定第二个布尔参数,设置编译时是否报错; 例: [Obsolet ...
- OC中在.h和.m中声明的属性和成员变量有何区别?
相比Swift而言,OC规矩太多. 差不多,.h中声明的属性和成员变量均可以在子类中访问到.而.m则不可.而属性其实也就是成员变量的一种简写,其内部自动包含了getter和setter方法. 如图:V ...
- silverlight属性改变事件通知
工作中遇到silverlight本身没有提供的某些属性改变事件,但又需要在属性改变时得到通知,Google搬运stack overflow,原地址 /// Listen for change of t ...
随机推荐
- Android之mtklog分析
Android之mtklog分析 [海外场测反馈][xxx]动态测试时对比机xxxx拨打测试机xxxxx自动挂断电话 工作中遇到一个掉话的问题,需要分析log,log比较大,我也没法上传,就简答的讲讲 ...
- openssl 非对称加密 RSA 加密解密以及签名验证签名
1. 简介 openssl rsa.h 提供了密码学中公钥加密体系的一些接口, 本文主要讨论利用rsa.h接口开发以下功能 公钥私钥的生成 公钥加密,私钥解密 私钥加密,公钥解密 签名:私钥签名 验 ...
- NSURLErrorDomain -999 "Canceled" 错误探究
完整错误描述为 Error Domain=NSURLErrorDomain Code=-999 "Canceled/已取消" 这个错误一般用来描述某个网络请求在还未被发出时就被意外 ...
- java/Android 接口调用的几种写法
虽然Handler用的地方比较普遍,但是接口也有他的独特之处,比较直观,然后降低了耦合性 如有一接口,需要将数据传给使用的activity中,接口如下 public interface PushVal ...
- 如何让同局域网的同事访问我电脑上的PHP网站和数据库
需求:想让公司同一局域网的同事电脑访问我的电脑里面的php项目. 条件:首先确认localhost正常访问你的本地项目 环境:我使用的是wampserver2.5集成环境 步骤: 1.增加新增监听端口 ...
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1.CURRENT_TIMESTAMP 当要向 ...
- javascript 之 prototype 浅析
prototype 原型 javascript 是一种 prototype based programming 的语言, 而与我们通常的 class based programming 有很大 的区别 ...
- 优化Google字体 全面加速WordPress
从5月27号起,由于某些原因,Google服务在大陆的崩溃影响了数百万的站长,因为很多wordpress主题都在使用Google的在线字体方案-google fonts包括新版的WordPress 后 ...
- uiimage 上传 数据库
之前我所接触的上传图片都是直接与服务器交互的,即 app端要做的就是上传到服务器 现在这个项目却是app先上传到"数据库",由"数据库"传到服务端 下面说主题 ...
- x64内联汇编注意点
#include <windows.h> #include <stdio.h> extern "C" int MyPrintf(ULONGLONG,ULON ...