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();
}
}
加支付宝好友偷能量挖...