简体   繁体   English

如何使用 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....

        }

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

Why you don't use NUnit test context?为什么不使用 NUnit 测试上下文?

You will be able to get all needed data and information about the test.您将能够获得有关测试的所有所需数据和信息。

See NUnit docs here .请参阅此处的 NUnit 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Reflection - 如何判断MethodInfo对象是Method还是Property访问器? - System.Reflection - How can I tell if a MethodInfo object is a Method or a Property accessor? system.reflection无法获取属性值 - system.reflection unable to get property value 如何使用 System.Reflection 中的 PEReader 从 DLLImport 属性获取 DLL 名称/路径? - How do I get the DLL name/path from the DLLImport attribute using PEReader from System.Reflection? System.Reflection - 如何调用次级类型的getter方法? - System.Reflection - How can I invoke the getter method of the sublevel Types? System.Reflection-如何获取子类型的PropertyInfo? - System.Reflection - How can I get the PropertyInfo of the sublevel Types? 使用System.Reflection - using System.Reflection 使用System.Reflection检索const字符串字段的列表 - Using System.Reflection to retrieve a list of const string fields C#如何在参数数量相等时使用System.Reflection调用私有重载方法 - C# How do I invoke a private overloaded method using System.Reflection when number of arguments are equal 如何避免使用System.Reflection在ComboBox中显示Enum属性值 - How to avoid using System.Reflection for displaying Enum attribute values in a ComboBox System.Reflection,如何获取字段&属性position? - System.Reflection, how to get field & property position?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM