因为
我从C#4.0复制了这段代码,简单地说,它使用Attribute/Reflection API来控制操作:运行的测试数量和显示错误消息。
当我运行代码时,我得到了这个结果。
Method Method1 will be tested; reps=1; msg=; memo=
Test1
Method Method2 will be tested; reps=3; msg=; memo=abc
Test2
Test2
Test2
Method Method3 will be tested; reps=3; msg=Debugging Time!; memo=
Test3
Test3
Test3
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at Hello.Main () [0x00000] in <filename unknown>:0
为什么是Unhandled Exception
?
源代码如下:
using System;
using System.Reflection;
[AttributeUsage (AttributeTargets.Method)]
public sealed class TestAttribute : Attribute
{
public int Repititions;
public string FailureMessage;
public string Memo;
public TestAttribute() : this(1) {}
public TestAttribute(int repititions) {Repititions = repititions;}
}
class Foo
{
[Test]
public void Method1()
{
Console.WriteLine("Test1");
}
[Test(3, Memo="abc")]
public void Method2()
{
Console.WriteLine("Test2");
}
[Test(3, FailureMessage="Debugging Time!")]
public void Method3()
{
Console.WriteLine("Test3");
}
}
class Hello
{
static void Main()
{
foreach (MethodInfo mi in typeof(Foo).GetMethods())
{
TestAttribute att = (TestAttribute) Attribute.GetCustomAttribute(mi, typeof(TestAttribute));
if (att != null)
Console.WriteLine("Method {0} will be tested; reps={1}; msg={2}; memo={3}", mi.Name, att.Repititions, att.FailureMessage, att.Memo);
for (int i = 0; i < att.Repititions; i++)
try
{
mi.Invoke(new Foo(), null);
}
catch (Exception ex)
{
throw new Exception ("Error: " + att.FailureMessage, ex);
}
}
}
}
您的if (att != null)
和foreach
中缺少一些大括号{}
。仅仅缩进是不够的。
class Hello
{
static void Main()
{
foreach (MethodInfo mi in typeof(Foo).GetMethods())
{
TestAttribute att = (TestAttribute) Attribute.GetCustomAttribute(mi, typeof(TestAttribute));
if (att != null)
{
Console.WriteLine("Method {0} will be tested; reps={1}; msg={2}; memo={3}", mi.Name, att.Repititions, att.FailureMessage, att.Memo);
for (int i = 0; i < att.Repititions; i++)
{
try
{
mi.Invoke(new Foo(), null);
}
catch (Exception ex)
{
throw new Exception ("Error: " + att.FailureMessage, ex);
}
}
}
}
}
}
MethodInfo mi
是从没有TestAttribute
的对象继承的ToString
。因此,现在att
的值为空,并尝试在其上调用att.Repititions
。您可以将其更改为
if (att != null)
{
Console.WriteLine("Method {0} will be tested; reps={1}; msg={2}; memo={3}", mi.Name, att.Repititions,
att.FailureMessage, att.Memo);
for (int i = 0; i < att.Repititions; i++)
try
{
mi.Invoke(new Foo(), null);
}
catch (Exception ex)
{
throw new Exception("Error: " + att.FailureMessage, ex);
}
}
修复它。
您可能遇到了一个没有属性的内部方法,因此此行失败:
TestAttribute att = (TestAttribute) Attribute.GetCustomAttribute(mi, typeof(TestAttribute));
你想使用安全铸造:
TestAttribute att = Attribute.GetCustomAttribute(mi, typeof(TestAttribute)) as TestAttribute;
我在代码中添加了一个存根行-您正试图从System.String的ToString()方法中获取自定义属性。
foreach (MethodInfo mi in typeof(Foo).GetMethods())
{
Console.WriteLine(mi.ToString());
TestAttribute att = (TestAttribute) Attribute.GetCustomAttribute(mi, typeof(TestAttribute));
if (att != null)
Console.WriteLine("Method {0} will be tested; reps={1}; msg={2}; memo={3}", mi.Name, att.Repititions, att.FailureMessage, att.Memo);
for (int i = 0; i < att.Repititions; i++)
try
{
mi.Invoke(new Foo(), null);
}
catch (Exception ex)
{
throw new Exception ("Error: " + att.FailureMessage, ex);
}
}