一段关于测试和自定义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 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
随机推荐
- Go语言并发编程示例 分享(含有源代码)
GO语言并发示例分享: ppt http://files.cnblogs.com/files/yuhan-TB/GO%E8%AF%AD%E8%A8%80.pptx 代码, 实际就是<<Go ...
- tab标签切换
<script>$(document).ready(function(){ $(".drtitle li").click(function(m){ $(this).ad ...
- Install and set JAVA home on MAC OS with commandline
最近需要在MAC上做一些测试,由于测试机没有安装Java,只能自己安装,由于不能通过图形化界面访问测试机,只能通过命令行的形式来安装JAVA. 1. Download the jre/jdk inst ...
- TableView遇到的问题
1.所建立的TableView滑动不到底部的问题: tableView继承scrollerView,当tableview开始建立的时候,会先计算每个cell的高度和每个headerview的高度.fo ...
- difference between forward and sendredirect
Difference between SendRedirect and forward is one of classical interview questions asked during jav ...
- ubuntu下avd创建和文件传输
进入安卓官网下载安卓sdk并解压到指定路径 在命令行模式下进入安卓文件的toos文件夹下 android avd #在弹出的对话框中新建虚拟机 adb install ~/targetPath/hel ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- CDH5X 安装oozie报错To enable Oozie web console install the Ext JS library.
最近在CDH5.X 安装oozie 服务,服务安装完毕,访问oozie server ui,报如下错误: 页面提示: Oozie web console is disabled.To enable O ...
- Django1.3 创建项目
经历了各种失败各种烦恼以后Django开发环境终于搭建好了! 系统环境:ubuntu12.04 Django版本1.3.1 Python版本 2.7.3 接下来就兴建一个项目练习一下 1.创 ...
- 昨天晚上画了个带apple的图:ide插件与php和xdebug通信原理图,周末写1个调试器。
昨天晚上画了个带apple的图:ide插件与php和xdebug通信原理图,周末写1个调试器.