2012-04-27 27 views
2

在我的解决方案,我有DLL包含以下格式提取额外的方法信息,从DLL的

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

一些方法方法有不同的优先级,所有者,ID &标题

通过提供DLL名称&检索算法标准(优先权,所有者,ID &标题),我可以得到在给定优先级组或所有者组等方法名称等。

我有代码,通过它我得到的细节我方法名称&使用的参数,但我没有得到如何从测试属性获取信息。

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

回答

1

这听起来像你只是在寻找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); 
} 

(这是假设的TestPropertyAttributeKeyValue性能,当然)。

要只是检测的该存在属性(您可能需要TestMethodAttribute),您可以使用MemberInfo.IsDefined

0

假设你已经有了MethodInfo对象(因为你说你已经有了获取信息的代码),你可以调用MethodInfo.GetCustomAttributes来获得这些属性。它也有一个超负荷的地方,你可以传递你寻找的属性的类型。然后您只需要投射结果并检查其属性。