一段关于测试和自定义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 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
随机推荐
- iOS学习之MVC模式
Modal 模型对象: 模型对象封装了应用程序的数据,并定义操控和处理该数据的逻辑和运算.例如,模型对象可能是表示商品数据 list.用户在视图层中所进行的创建或修改数据的操作,通过控制器对象传达出去 ...
- iOS学习之单例模式
单例模式(Singleton) 概念:整个应用或系统只能有该类的一个实例 在iOS开发我们经常碰到只需要某类一个实例的情况,最常见的莫过于对硬件参数的访问类,比如UIAccelerometer.这个类 ...
- ajax容易忽视的细节
用了很长时间的ajax,自己也写过原生ajax请求,但是发现自己对于ajax理解仍然非常肤浅. 1.ajax请求后,服务器会返回数据,返回头中content-type直接影响responseXML,r ...
- linux中解压缩并安装.tar.gz后缀的文件
1.解压缩: Linux下以tar.gz为扩展名的软件包,是用tar程序打包并用gzip程序压缩的软件包.要安装这种软件包,需要先对软件包进行解压缩,使用“tar -zxfv filename.tar ...
- seajs封装js方法
必须要先引入sea.js文件 <script src="js/sea.js"></script> 其次,引入其他js文件 <script> se ...
- 【Python】使用正则表达式实现计算器练习
已知有以下这样一个不太友好的公式: 1 - 2 * ( (60-30 +(-9-2-5-2*3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 ...
- oracle xmltype导入并解析Excel数据 (五)中间表数据入库
此处给出例子,具体根据业务需求 create or replace procedure P_CART_Sheet1(p_id in NUMBER) is--车辆管理功能v_str varchar2(4 ...
- window 下如何安装ghost博客
1.安装nodejs # Node v0.12.x and v4.2+ LTS - supported 我本地安装的是4.2 安装其他版本可能提示系统不兼容 2.安装mysql 3.安装bower 4 ...
- matlab mesh visualization
1. matlab color specification http://au.mathworks.com/help/matlab/ref/colorspec.html
- (转)CSS中的绝对定位与相对定位定位
层级关系为: <div ——————————— position:relative; 不是最近的祖先定位元素,不是参照物<div—————————-没有设置为定位元素,不是参照物<d ...