Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Get-Member for reflection

I'd basically like to use reflection in Powershell, and after dynamically finding the methods I'd like to invoke using standard powershell commands, I ended up with a MemberDefinition object and not much clue as to how to invoke it. I'm not positive I can, so if you have experience you can just say not to do it this way. I know I can drop into the Assembly namespace, but didn't know if I could do something like this:

$method = $ie | get-member -type method | ? { $_.name -eq 'span' }
invoke-member $ie $method

The $method variable is of type Microsoft.PowerShell.Commands.MemberDefinition, is this even possible?

Thanks, Matthew

like image 663
mlhDev Avatar asked Dec 21 '22 00:12

mlhDev


1 Answers

Sounds like you're pretty familiar with .NET, why not just drop down and use reflection directly?

PS> $d = Get-Date
PS> $t = $d.GetType()
PS> $t.InvokeMember("ToUniversalTime", "Public,InvokeMethod,Instance", $null, $d, $null)

Saturday, March 31, 2012 3:10:51 AM
like image 61
Keith Hill Avatar answered Jan 08 '23 08:01

Keith Hill