2009-05-22 70 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