=波波日志 > Asp.Net/C#/WCF > C#自定义属性=

[转]C#自定义属性

C#自定义属性实例代码
+展开
-C#
using System;
using System.Threading;
using System.Reflection;

[AttributeUsage(AttributeTargets.Method,AllowMultiple=false)]
class TestAttribute:System.Attribute
{
    public TestAttribute()
    {
        Console.WriteLine("TestAttribute.ctor() Default ctor.");
    }

    public TestAttribute(int nTime)
    {
        Console.WriteLine("TestAttribute.ctor(int)");
        m_nTime = nTime;
    }

    private int m_nTime = 1;
    private bool m_Ignore = false;
    public bool Ignore
    {
        get { return m_Ignore; }
        set { m_Ignore = value; }
    }

    public int nTime
    {
        get { return m_nTime; }
        set { m_nTime = value; }
    }

}

class program
{
    static void Main()
    {
        TestAssembly(Assembly.GetExecutingAssembly());
    }

    static void TestAssembly(Assembly assembly)
    {
        foreach (Type type in assembly.GetTypes())
        {
            foreach (MethodInfo method in type.GetMethods())
            {
                Object[] attributes = method.GetCustomAttributes(typeof(TestAttribute), false);
                if (attributes.Length==1)
                {
                    TestAttribute testAttribute = attributes[0] as TestAttribute;
                    if (!testAttribute.Ignore)
                    {
                        Object[] parameters = new Object[0];
                        Object instance = Activator.CreateInstance(type);
                        for (int i = 0; i < testAttribute.nTime; i++)
                        {
                            try
                            {
                                method.Invoke(instance, parameters);
                            }
                            catch (TargetInvocationException ex)
                            {
                                Console.WriteLine("The method {" + type.FullName+"."+ method.Name+
                                    "} throw an exception of type" + ex.InnerException.GetType()+
                                    " during run #" + (i+1)+".");
                            }
                        }
                    }
                }
            }
        }
    }
}

class Foo
{
    [Test()]
    public void Crash()
    {
        Console.WriteLine("Crash()");
        throw new ApplicationException();
    }

    int state = 0;

    [Test(4)]
    public void CrashTheSendTime()
    {
        Console.WriteLine("CrashTheSecondTime()");
        state++;
        if (state==2)
        {
            throw new ApplicationException();
        }
    }

    [Test()]
    public void DontCrash()
    {
        Console.WriteLine("Dontcrash()");
    }

    [Test(Ignore = true)]
    public void CrashButIgnored()
    {
        Console.WriteLine("CrashButIngnored()");
        throw new ApplicationException();
    }
}
类别:Asp.Net/C#/WCF 作者:转载 日期:2010-12-03 【评论:0】 
 
暂时没有评论!
发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012,桂ICP备05005887号 京公网安备1101055090