如何使用 System.Reflection 检索 NUnit 测试方法的 "Property" 属性值?

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....

        }

我正在使用 C# 中的 System.Reflection 读取此方法的属性。但是,我无法读取 "Property" 属性的值,即 "TestDescription"、"Testing Subtraction of Two numbers"。我还需要读取 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.
         }

       }
   } 
}

您可以使用Attribute.GetCustomAttributes获取所有信息。 PropertyAttribute 有点棘手,因为您可以为一个键分配多个值。这是一个例子:

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}");
                }
            }
        }
    }
}

为什么不使用 NUnit 测试上下文?

您将能够获得所有需要的数据和有关测试的信息。

参见 NUnit 文档 here