2012-04-27 25 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來獲得這些屬性。它也有一個超負荷的地方,你可以傳遞你尋找的屬性的類型。然後您只需要投射結果並檢查其屬性。