=波波日志 > 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();
}
}
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】
相关文章
暂时没有评论!
发表留言
热门博文
- IE里Cookie跨域不能读取
- web服务因URL意外地以/**结束,请求格式无法识别
- 去掉隐藏asp.net编译出错aspxerrorpath错误参数
- 解决asp.net验证视图状态 MAC 失败
- 找不到System.Web.Script.Services.ScriptService
- 在aspx,ashx页面挂起线程执行
最新博文
- WCF利用限流(Throttling)控制并发访问[下篇]
- WCF利用限流(Throttling)控制并发访问[上篇]
- ConcurrencyMode.Multiple模式下的WCF服务同步上下文对并发的影响[下篇]
- ConcurrencyMode.Multiple模式下的WCF服务同步上下文对并发的影响[上篇]
- WCF基于ConcurrencyMode.Reentrant模式下的并发控制机制
- C#如何设置标记方法等为否决的不可用
随机博文
- C#通讯编程--remoting
- 防止.NET木马列所有站物理路径,防止.JPG类型木马
- ashx是什么文件,如何创建
- C# 3.0 Select操作
- 用C#发送手机短信
- ConcurrencyMode.Multiple模式下的WCF服务同步上下文对并发的影响[上篇]
广告商赞助

