Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Invoke generic method from PowerShell

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 06-08-2007   #1 (permalink)
zach.blocker@gmail.com
Guest


 

Invoke generic method from PowerShell

How does one invoke a generic method from PowerShell?

For example, let's say I have an assembly MyAssembly.dll with a public
class MyClass. MyClass has (wait for it) MyGenericMethod<T>().

Let's say in PowerShell I load the assembly and get an instance of
MyClass:

[System.Reflection.Assembly]::LoadFrom("MyAssembly.dll")
$MyClassInstance = New-Object MyAssembly.MyClass

How do I invoke the generic method? I'm assuming the following is not
correct:

# Likely incorrect:
$MyClassInstance.MyGenericMethod<[string]>()


My System SpecsSystem Spec
Old 06-08-2007   #2 (permalink)
Keith Hill [MVP]
Guest


 

Re: Invoke generic method from PowerShell

<zach.blocker@gmail.com> wrote in message
news:1181311127.954899.22060@g4g2000hsf.googlegroups.com...
> How does one invoke a generic method from PowerShell?


Take a look at this blog post by Lee Holmes:

http://www.leeholmes.com/blog/Creati...owerShell.aspx

--
Keith

My System SpecsSystem Spec
Old 06-08-2007   #3 (permalink)
zach.blocker@gmail.com
Guest


 

Re: Invoke generic method from PowerShell

I should amend the above to say that I did more than assume it wasn't
correct. I tested it at the prompt and got an error message:
The redirection operator '<' is not supported yet.

My System SpecsSystem Spec
Old 06-08-2007   #4 (permalink)
zach.blocker@gmail.com
Guest


 

Re: Invoke generic method from PowerShell

On Jun 8, 10:14 am, "Keith Hill [MVP]"
<r_keith_h...@mailhot.nospamIdotcom> wrote:
> Take a look at this blog post by Lee Holmes:
>
> http://www.leeholmes.com/blog/Creati...owerShell.aspx
> --
> Keith


That is a brilliant little script he's got. However, he is
instantiating a generic type. My example class above (MyClass) is
*not* a generic type, but it does have a generic method. Since it's
not a generic type, I can instantiate it easily using "New-Object",
however, how do I invoke a generic method on it?

My System SpecsSystem Spec
Old 06-08-2007   #5 (permalink)
Mesan
Guest


 

Re: Invoke generic method from PowerShell

On Jun 8, 7:25 am, zach.bloc...@gmail.com wrote:
> On Jun 8, 10:14 am, "Keith Hill [MVP]"
>
> <r_keith_h...@mailhot.nospamIdotcom> wrote:
> > Take a look at this blog post by Lee Holmes:

>
> >http://www.leeholmes.com/blog/Creati...owerShell.aspx
> > --
> > Keith

>
> That is a brilliant little script he's got. However, he is
> instantiating agenerictype. My example class above (MyClass) is
> *not* agenerictype, but it does have agenericmethod. Since it's
> not agenerictype, I can instantiate it easily using "New-Object",
> however, how do I invoke agenericmethodon it?


Zach - Have you been able to find a solution? I too would very much
like to know how to all generic methods from a non-generic instance.

My System SpecsSystem Spec
Old 06-08-2007   #6 (permalink)
zach.blocker@gmail.com
Guest


 

Re: Invoke generic method from PowerShell

Nothing yet, Mesan. I'm hoping someone can post a reply here. Of
course, if I find a solution offline, I will post it here as well.

My System SpecsSystem Spec
Old 06-14-2007   #7 (permalink)
jeff.hillman@gmail.com
Guest


 

Re: Invoke generic method from PowerShell

On Jun 8, 10:18 pm, zach.bloc...@gmail.com wrote:
> Nothing yet, Mesan. I'm hoping someone can post a reply here. Of
> course, if I find a solution offline, I will post it here as well.


Given this class:

using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary
{
public class Class
{
private string id;

public Class( string id )
{
this.id = id;
}

public void GenericMethod<T, G>()
{
Console.WriteLine( string.Format( "{0}'s T is a: {1}", id,
typeof( T ).Name ) );
Console.WriteLine( string.Format( "{0}'s G is a: {1}", id,
typeof( G ).Name ) );
}
}
}

Once the assembly is loaded, GenericMethod can be called in this way:

$classInstance = New-Object ClassLibrary.Class "something"

[Type] $classType = $classInstance.GetType()

[Reflection.MethodInfo] $methodInfo =
$classType.GetMethod( "GenericMethod" )

$genericMethodInfo = $methodInfo.MakeGenericMethod( [Type[]]
@( "".GetType(), (0).GetType() ) )

$genericMethodInfo.Invoke( $classInstance, $null )

I hope this helps.

My System SpecsSystem Spec
Old 06-25-2007   #8 (permalink)
zach.blocker@gmail.com
Guest


 

Re: Invoke generic method from PowerShell

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Powershell Invoke-Item and cmd line parameters Damian PowerShell 1 05-06-2008 07:33 PM
Powershell missing a WMI method? Guy PowerShell 1 07-27-2007 01:32 PM
Add method not accessibe from Powershell? joeOnSunset PowerShell 4 05-31-2007 04:35 PM
Re: IsUpper method in PowerShell? Duncan Smith PowerShell 4 03-29-2007 04:46 AM
How to use HTMLDecode method in Powershell? Vinicius Canto PowerShell 11 08-02-2006 07:13 PM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008
Page generated in 0.27460 seconds with 10 queries