一段关于测试和自定义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 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...
随机推荐
- C语言程序设计第13次作业
一.本次课主要内容: 本章主要介绍结构的基本概念与定义.结构变量的定义和使用.结构数组应用.结构指针概念.重点介绍结构的定义与成员引用方式.同时也介绍了文件的基本概念,文件操作处理基本步骤和过程,常用 ...
- GridView导出excel格式问题
在导出的点击事件中,代码如下: //指定导出对应单元格为文本样式 string style = @"<style> .test { vnd.ms-excel.numberform ...
- CodeVS 线段覆盖1~5
#include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...
- ios--个人资料修改
点击进行编辑  (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) ...
- ASP.NET项目部署到Linux服务器出现服务器错误
在Linux系统中安装了Mono和Apache作为Web服务器,使用Visual Studio开发的ASP.NET Web应用或者API应用,在部署到Linux服务器后出现服务器错误,其中一个原因是由 ...
- IOS开发官方文档随笔
马上着手开发IOS应用程序 创建第一个单视图应用 ###main 方法 int main(int argc, char * argv[]) { @autoreleasepool { return UI ...
- C++学习笔记28:运行期型式信息
RTTI 运行期标识对象的型式信息 优势:允许使用指向基类的指针或引用自如地操作派生类的对象 typeid:获取表达式的型式:type_info:型式信息类 头文件:typeinfo 对象转型模板 d ...
- devexpress xaf 开发中遇到的问题.
devexpress xaf 开发中遇到的问题很多久了就忘记了.每天都把开发内容记录下来,方便大家,方便自己
- forEach遍历数组实例
forEach()取出数组中2的倍数和3的倍数的数 //for IE if(!Array.prototype.forEach){ Array.prototype.forEach = function( ...
- MySQL 重装
由于之前第一次装MySQL,默认的datadir在启动盘中,我想要将datadir移动到更大的存储盘中.无奈网上的各种文章的方法在我这里总是不work.我决定重新用homebrew来装一遍MySQL. ...