Tutorial :Query assembly


I have a .NET assembly that has tens of classes and methods which are unit testing methods. I want to generate a report with all the method marked with attribute Ignore, do you know a simple way to do that?


You want the get Custom Attributes method

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

This method also exists on the method object, so you can iterate through all the types in your assembly and iterate through all their methods, and call the same method.

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

Edit, Here is some help with the powershell syntax, although I must say, I am NOT powershell fluent. I am sure someone can do this way better than the crap I have below.

$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      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »