一段关于测试和自定义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() != 999)
                throw new Exception("Pesky Kids");
        }
    }

    [XXX(typeof(TestAnObject))]
    public class SomeCodeOrOther
    {
        public SomeCodeOrOther()
        {

        }

        public int Do()
        {
            return 999;
        }
    }

    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 == 1)
                {
                    Console.WriteLine("  Found XXXAttribute: Running Test");
                    XXXAttribute tca = atts[0] as XXXAttribute;

                    try
                    {
                        tca.Test();
                        Console.WriteLine("PASSED");
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("FAILED");
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            Console.Read();
        }
    }
}

一段关于测试和自定义Attribute的代码

时间: 2024-08-01 10:28:39

一段关于测试和自定义Attribute的代码的相关文章

转:C#制作ORM映射学习笔记一 自定义Attribute类

之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己的小项目吗管他的,自己爽就行了).不过现在世面上的ORM映射基本都是为web项目设计的,与unity项目很难契合,所以我决定自己做一个简易的ORM映射.虽然想的很美但是实际做起来才发现还是挺复杂的,所以我在这里记录一下这次的开发过程,防止以后遗忘. 今天先记录一下如何通过自定义attribute类实

C#自定义Attribute值的获取与优化

C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute 1 [AttributeUsage(AttributeTargets.All)] 2 public sealed class NameAttribute : Attribute 3 { 4 private readonly string _name; 5 6 public string Name 7 { 8 get { return _na

CVS导出&&自定义Attribute的使用

1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class CvsExportHelper { /// <summary> /// Creates the CSV from a generic list. /// </summary>; /// <typeparam name="T"></typeparam&

XsdGen:通过自定义Attribute与反射自动生成XSD

前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML效率要高,但还是有些学习成本的.此外XML的tag太多,如果设计的类型属性过多,手写XSD也不太现实,很难专注于设计. 于是我想能不能先用C#写好类型,然后自动生成标准格式的XSD呢.经过三天左右的设计和实现,目前实现了以下功能: 1. 支持Class和Enum类型的设计 2. 支持基元类型.自定义

获取自定义Attribute的Description

自定义Attribute /// <summary> /// 合同状态 /// </summary> public enum ContractStatus { [GlobalCode("新建")] Pending = 0, [GlobalCode("提交待审批")] AuditPending = 3, [GlobalCode("审批拒绝")] AuditReject = 6 } [AttributeUsage(Attrib

C#自定义Attribute

Attribute 类被称为特性.是一种可由用户自由定义的修饰符(Modifier),可以用来修饰各种需要被修饰的目标.可以修饰类,接口,属性,方法等.它不同于注释,注释在程序被编译的时候会被编译器所丢弃,因此,它丝毫不会影响到程序的执行.而Attribute是程序代码的一部分,不但不会被编译器丢弃,而且还会被编译器编译进程序(Assembly)的元数据(Metadata)里,在程序运行的时候,你随时可以从元数据里提取出这些附加信息来决策程序的运行. Framework中我们有时候调用方法,会提

.net c#获取自定义Attribute

前言: 在c#开发中,有时候我们需要读取 Attribute中的信息(关于Attribute , 我自己把他理解成一个可以为类,属性标记的东西,这个标记可以为你提供一些关于类,方法,属性的额外信息) 我们如何获取这些标记的信息,如何获取自定义Attribute信息. 正文: 1.获取一个枚举的详细信息 假设我们有这样一个枚举 public enum Category { /// <summary> /// 英语 /// </summary> English, /// <sum

c#自定义Attribute获取接口实现

原文:c#自定义Attribute获取接口实现 一般的接口实现多态 定义接口 interface Ipeople { void say(); } 定义实现的类 public class man : Ipeople { public void say() { MessageBox.Show("man"); } } public class woman : Ipeople { public void say() { MessageBox.Show("woman"); }

自定义Attribute,使用记录

1. 自定义attribute需要继承Attribute基类 /// <summary> /// Datareader反射使用 标记 /// 标记 属性在Datareader 中的字段名 以便反射赋值 /// </summary> [AttributeUsage(AttributeTargets.Property| AttributeTargets.Field)] public class DataReaderModelAttribute:Attribute { private s