从 DLL 中提取其他方法信息

本文关键字:方法 信息 其他 提取 DLL | 更新日期: 2023-09-27 18:32:21

在我的解决方案中,我有dll,其中包含以下格式的方法

    [TestMethod]
    [TestProperty("Priority", "P0")]
    [TestProperty("Owner", "vbnmg")]
    [TestProperty("Title", "Verify the log accessible")]
    [TestProperty("ID", "1")]
    public void LogAccesiblityTest()
    {
    //Test Code
    }

某些方法具有不同的优先级,所有者,ID和标题

通过提供dll名称和serach标准(优先级,所有者,ID和标题),我可以获取给定优先级分组器或所有者组等中的方法名称。

我有代码,通过它获取所用方法名称和参数的详细信息,但我不知道如何从测试属性获取信息。

有人可以建议如何做到这一点。

从 DLL 中提取其他方法信息

听起来你只是在寻找MethodInfo.GetCustomAttributes.鉴于您的格式,我可能会写这样的东西:

public static Dictionary<string, string> GetProperties(MethodInfo method)
{
    return method.GetCustomAttributes(typeof(TestPropertyAttribute), false)
                 .Cast<TestProperty>()
                 .ToDictionary(x => x.Key, x => x.Value);
}

(当然,这是假设TestPropertyAttribute具有KeyValue属性。

要仅检测属性的存在(您可能希望TestMethodAttribute),您可以使用MemberInfo.IsDefined

假设你已经有MethodInfo对象(因为你说你已经有代码来获取信息),你可以调用MethodInfo.GetCustomAttributes来获取这些属性。它还有一个重载,您可以在其中传递要查找的属性的类型。然后,您只需强制转换结果并检查其属性。