一段关于测试和自定义Attribute的代码
来自《西夏普入门经典》
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace ConsoleApplication1
{ [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class XXXAttribute : Attribute
{
public XXXAttribute(System.Type testCase)
{
TestCase = testCase;
} public readonly System.Type TestCase; public void Test()
{
object o = Activator.CreateInstance(TestCase);
}
} public class TestAnObject
{
public TestAnObject()
{
SomeCodeOrOther scooby = new SomeCodeOrOther();
if (scooby.Do() != )
throw new Exception("Pesky Kids");
}
} [XXX(typeof(TestAnObject))]
public class SomeCodeOrOther
{
public SomeCodeOrOther()
{ } public int Do()
{
return ;
}
} class Program
{
static void Main(string[] args)
{ Assembly A = Assembly.GetExecutingAssembly();
System.Type[] types = A.GetExportedTypes(); foreach (System.Type t in types)
{
Console.WriteLine("Checking type {0}", t.ToString()); object[] atts = t.GetCustomAttributes(typeof(XXXAttribute), false); if (atts.Length == )
{
Console.WriteLine(" Found XXXAttribute: Running Test");
XXXAttribute tca = atts[] as XXXAttribute; try
{
tca.Test();
Console.WriteLine("PASSED");
}
catch (System.Exception ex)
{
Console.WriteLine("FAILED");
Console.WriteLine(ex.Message);
}
}
} Console.Read();
}
}
}
一段关于测试和自定义Attribute的代码的更多相关文章
- C#自定义Attribute值的获取与优化
C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...
- 转:C#制作ORM映射学习笔记一 自定义Attribute类
之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...
- WPF自定义RoutedEvent事件代码段
今天在写东西的时候,发现常用的代码段里没有RoutedEvent的,因此,写了一个代码段,方便以后使用,顺便记录一下,如何做代码段. 1.在项目中新建一个XML文件,将扩展名修改为snippet. 2 ...
- XsdGen:通过自定义Attribute与反射自动生成XSD
前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...
- 2.C#自定义Attribute
阅读目录 一:C#自定义Attribute 二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets 三:Attribut ...
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- .net c#获取自定义Attribute
前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) ...
- CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...
- 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻
原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
随机推荐
- sphinx,coreseek安装
sphinx是国外的一款搜索软件. coreseek是在sphinx的基础上,增加了中文分词功能,换句话说,就是支持了中文. Coreseek发布了3.2.14版本和4.1版本,其中的3.2.14版本 ...
- django个人总结
今天是周末,多写写吧,和大家分享交流一下,虽园子django使用者不多,但算作自己的自我提升吧 django的网站建站的一点点自己的建议. 1:隐藏自己的前端行为或者能后端做的尽量不要在前端进行. 如 ...
- HTML标签整理
第一次接触动态网站的相关代码,对程序里HTML的标签不理解.在这里会把碰到的HTML标签整理出来,持续更新. 1.<form></form>:用于声明表单,定义采集数据的范围, ...
- datatables ajax后端请求那些坑
在对datatables做后端数据填充的时候,遇到一个,翻页问题.在多次操作翻页后,总是提示加载中.反了很多博客没有找到原因. 经过测试,发现原来自己坑了自己. 代码如下: datatables : ...
- C#通过WatiN操作页面中内嵌的Iframe
通过WatiN.Core.Broswer.Frame()方法来获取iframe对象,之后的容器就是frame,然后进行操作. 下面的例子是登录QQ空间的: Frame frame = browser. ...
- C# BackgroundWorker的使用
文章摘自:http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html BackgroundWorker 可以用于启动后台线程. 主要 ...
- New Training Table
2014_8_15 CodeForces 261 DIV2 A. Pashmak and Garden 简单题 B. Pashmak and Flowers 简单题 C. P ...
- 富文本编辑器kindeditor配置
<!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...
- Linux使用mount挂载samba共享
# 挂载smb使用读写777权限sudo mount -t cifs -o "rw,dir_mode=0777,file_mode=0777,username=winuser,passwor ...
- Base64简介
Base64是一种用64个字符来表示任意二进制数据的方法.首先,准备一个包含64个字符的数组:['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... ...