I have an NUnit test method which looks like this
[Test]
[Property("TestDescription","Testing Subtraction of Two numbers")]
[NUnit.Framework.CategoryAttribute("mytag,subtract")]
public void TestSubtract()
{
int res = SimpleCalculator.Subtract(10,10);
//some lines of code....
}
我正在使用C#中的System.Reflection读取此方法的属性。但是,我无法读取“属性”属性的值,即“ TestDescription”,“测试两个数字的减法”。我还需要读取CategoryAttribute的值。到目前为止,我还无法读取这些值。请帮助我。
这是我下面的代码。我正在从dll加载程序集。 然后,加载所有类型。对于每种类型,我正在检索methodInfo。 对于每个methodInfo,我都在检索属性。检索后 “ NUnit.Framework.PropertyAttribute”。我需要获取它的值。
Assembly a = Assembly.LoadFile(dllPath);
var types = a.GetTypes();
foreach(Type type in types)
{
foreach (MethodInfo methodInfo in type.GetMethods())
{
var attributes = methodInfo.getcustomAttributes(true);
foreach (var attr in attributes)
{
if ((attr.ToString() == "NUnit.Framework.TestAttribute") || (attr.ToString() ==
"NUnit.Framework.TestCaseAttribute"))
{
//some code
}
else if((attr.ToString() == "NUnit.Framework.PropertyAttribute"))
{
//** need to retrieve the attribute value here.
}
}
}
}