Marc
Marc

Reputation: 352

Powershell: list members of a .net component

I'm trying to build a script that will provide me the listing of the method of a dll from a .net component.

Here's what I've done so far:

Param([String]$path)
if($path.StartsWith("["))
{
$asm = [Reflection.Assembly]::LoadWithPartialName($path)
$asm.GetTypes() | select Name, Namespace | sort Namespace | ft -groupby Namespace
}
else
{
$asm = [Reflection.Assembly]::LoadFile($path)
$asm.GetTypes() | select Name, Namespace | sort Namespace | ft -groupby Namespace
}

So basically the second part of the script (when providing the path of a dll) is working perfectly fine;

Running '.\GetDllMethods.ps1 -path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"' will provide me with all the members of the WinSCP dll.

What I want to achieve with the first part of the script is to get the same result by providing the name of the .net component using something like:

.\GetDllMethods.ps1 -path "[System.IO.StreamWriter]"

To get all the member of the StreamWriter component.

But I'm getting a null exception here.. Any hint ?

Upvotes: 7

Views: 20460

Answers (2)

ojk
ojk

Reputation: 2542

While the other answer is technically correct, they don't answer your question of why the first part of your script is not working.

I think it's a two part reason:

  1. If the assembly is already loaded, it will probably result in a null-value response
  2. The syntax for the LoadWithPartialName method don't use the square brackets, so you need to filter those out of the string, or don't supply them in the first place

You can check if the assembly is already loaded with the following:

[Reflection.Assembly]::GetAssembly('assemblyName')

Wrap it in a try-catch to handle the error if the assembly isn't loaded/found.

Upvotes: 3

Loïc MICHEL
Loïc MICHEL

Reputation: 26150

you can use the powershell cmdlet get-member

PS>[system.net.webclient]|get-member  -MemberType method                                                                              


   TypeName : System.RuntimeType                                                                                

Name                           MemberType Definition                                                            
----                           ---------- ----------                                                            
AsType                         Method     type AsType()                                                         
Clone                          Method     System.Object Clone(), System.Object ICloneable.Clone()               

...

we can see there is a GetMethods method, so try :

[system.net.webclient].GetMethods()    

Upvotes: 6

Related Questions