![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 Specs![]() |
| | #7 (permalink) |
| 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 Specs![]() |
| | #8 (permalink) |
| Guest | Re: Invoke generic method from PowerShell |
My System Specs![]() |
![]() |
| 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 |