Shaggydog
Shaggydog

Reputation: 3788

How do I find out if a class and a method exist in .NET dll, from powershell?

I'm writing a powershell script that will remove redundant entries from my GlobalSuppressions.cs file. One of the techniques I want to use is to check whether the class and method the entry refers to exist. The suppression entry looks like this

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("...", "...", Scope = "member", Target = "MyNamespace.Myclass#MyMethod(Namespace.ParameterType1,... Namespace.ParameterTypeN)...

from which I can extract fully qualified name of the class, and the method signature. I can load the dll from powershell. But I can't figure out how to ask "Does this class exist?" And "if it does, does it contain a method with this exact signature?" I guess this could be somehow achieved through reflection, but so far I have no idea how. I have one additional limitation, if possible, I need to perform the check without instantiating the class. This script needs to be universal, it will be run on many projects. There's no telling what classes will be checked, what code their default constructors will execute, or if they even have a default constructor with no parameters. Oh, and if you know a solution in C#, please share it, there's a good chance I'll be able to translate it into powershell.

Upvotes: 1

Views: 5192

Answers (1)

I'm not sure I fully understand your question but if you already have the assembly, GetTypes will list all types (public and private) contained in the assembly.

$assembly = [System.Reflection.Assembly]::LoadWithPartialName("System.Xml")
$assembly.GetTypes() | where-object { $_.name -eq "XmlNode" }

You can then call GetMembers or GetMethods to list the members/methods for a given type.

See http://msdn.microsoft.com/en-us/library/System.Type_methods.aspx for the detailed API.

Upvotes: 2

Related Questions