[英]How can I retrieve the value of the “Property” attribute of an NUnit Test Method using System.Reflection?
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....
}
I am reading the attribute of this method using System.Reflection in C#.我正在使用 C# 中的 System.Reflection 读取此方法的属性。 But, I have not been able to read the value of the "Property" attribute which is "TestDescription", "Testing Subtraction of Two numbers".
但是,我无法读取“属性”属性的值,即“TestDescription”、“测试两个数字的减法”。 I also need to read the value of the CategoryAttribute.
我还需要读取 CategoryAttribute 的值。 So far I have not been able to read the values.
到目前为止,我还无法读取这些值。 Please help me on this.
请帮助我。
This is my code below.这是我下面的代码。 I am loading the assembly from a dll.
我正在从 dll 加载程序集。 Then, loading all the types.
然后,加载所有类型。 For each types I am retrieving the methodInfo.
对于每种类型,我都在检索 methodInfo。 For each methodInfo I am retrieving the attributes.
对于每个 methodInfo,我都在检索属性。 After retrieving the "NUnit.Framework.PropertyAttribute".
检索“NUnit.Framework.PropertyAttribute”后。 I need retrieve its value.
我需要检索它的值。
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.
}
}
}
}
You can use Attribute.GetCustomAttributes
to get all information.您可以使用
Attribute.GetCustomAttributes
来获取所有信息。 PropertyAttribute
is a little bit tricky, because you can have multiple values assigned to one key. PropertyAttribute
有点棘手,因为您可以为一个键分配多个值。 Here is an example:这是一个例子:
using NUnit.Framework;
using System;
using System.Reflection;
namespace ConsoleApp
{
static class Program
{
static void Main(string[] args)
{
string dllPath = @"C:\Path\To\MyDll.dll";
Assembly a = Assembly.LoadFrom(dllPath);
Type[] types = a.GetTypes();
foreach (Type type in types)
{
foreach (MethodInfo methodInfo in type.GetMethods())
{
PropertyAttribute[] propertyAttributes = (PropertyAttribute[])Attribute.GetCustomAttributes(methodInfo, typeof(PropertyAttribute));
foreach (PropertyAttribute attribute in propertyAttributes)
foreach (string key in attribute.Properties.Keys)
foreach (var value in attribute.Properties[key])
Console.WriteLine($"PropertyAttribute :: Key: {key} :: Value: {value}");
CategoryAttribute[] categoryAttributes = (CategoryAttribute[])Attribute.GetCustomAttributes(methodInfo, typeof(CategoryAttribute));
foreach (CategoryAttribute attribute in categoryAttributes)
Console.WriteLine($"CategoryAttribute :: Name: {attribute.Name}");
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.