2009-05-22 30 views
2

我有一個.NET程序集,它有幾十個類和方法,它們是單元測試方法。 我想生成一個報告,所有的方法標記爲屬性忽略, 你知道一個簡單的方法來做到這一點嗎?查詢程序集

回答

6

你想要得到Custom Attributes方法

Assembly ass = Assembly.Load("yourassembly.dll"); 
object[] attributes = ass.GetCustomAttributes(typeof(IgnoreAttribute), false)); 

這種方法也存在方法的對象上,這樣你就可以通過在你的程序集中的所有類型的循環,並通過他們所有的方法迭代,並調用相同的方法。

foreach(Type type in ass.GetTypes()) { 
    foreach(MethodInfo method in type.GetMethods()) { 
     method.GetCustomAttributes(typeof(IgnoreAttribute), true)); 
    } 
} 

編輯,這裏有一些PowerShell語法的幫助,雖然我必須說,我不是PowerShell流利。我相信有人可以做到這一點比我下面的廢話更好。

$types = [System.Reflection.Assembly]::LoadFile("C:\dll.dll").GetTypes() 
$attribute = [System.Type]::GetType("IgnoreAttribute") 
foreach ($type in $types) { 
    $methods = $type.GetMethods() 
    foreach ($method in $methods) { 
    if ($method .GetCustomAttributes($attribute).Length -gt 0) { 
     $method.Name 
    } 
} 
+0

非常感謝 這是有幫助的,但我可以在Powershell中做到嗎? 我想把上面的語法翻譯成PowerShell – 2009-05-22 15:58:21