keyongtech


  keyongtech > windows.* > windows.powershell > 12/2008

 #1  
12-23-08, 11:07 PM
BatchMan
How to retrieve a generic method with GetMethod

Code
-------------------

#public static void Sort<T>( T[] array, Comparison<T> comparison
[System.Reflection.MethodInfo] $Methode = [System.Array].GetMethod("Sort",@([System.Array],[System.Comparison``1])
$Methode -eq $null
# return Tru

#OK : [System.Array].GetMethod("Sort",@([System.Array],[System.int32],[system.int32]) )
#NOK : [System.Array].GetMethod("Sort",@([System.Array],[System.Collections.Generic.IComparer``1])

-------------------

Thanks
 #2  
12-24-08, 07:43 AM
Josh Einstein
That's been a problem ever since generics were introduced. Since they were
kinda "retrofitted" into the CLR, it's not surprising that there are some
areas that don't quite play nicely with them.

Unfortunately your only option is to use GetMethods like:

function GetMethodEx([Type]$Type,[String]$Signature) {
$Type.GetMethods('Public, Static') | ?{ $_.ToString() -eq $Signature }
}

GetMethodEx Array 'void sort[t](t[], system.comparison`1[t])'

Hope this helps.

Josh Einstein

"BatchMan" <guest> wrote in message
news:9cde
[..]
 #3  
12-24-08, 11:08 AM
BatchMan
Josh Einstein;919089 Wrote:
>
> Hope this helps
>

Yes, absolutely
Thank you :

Code
-------------------

function GetMethodEx([Type]$Type,[String]$Signature)
{ $Type.GetMethods('Public, Static') | ?{ $_.ToString() -eq $Signature }

[System.Collections.HashTable[]] $T=@(
@{Nom="Pierre";Age=40}
@{Nom="Paul";Age=50}
@{Nom="Jacque";Age=30}
@{Nom="Pail";Age=20}
@{Nom="Pierrot";Age=5}
@{Nom="Alex";Age=78


$SBCompareHT
{ #Implémente le délégué : System.Comparison<T>( T x, T y
Write-Debug "Call Comparison
$x=$Args[0].Nom #Le contenu du champ Nom peut être égale à $null, compareTo gére ce ca
Write-Debug $($X
$y=$Args[1].No
Write-Debug $($Y
if ($x -eq $null

if ($y -eq $null

#If x is null and y is null, they're equal.
Write-debug "Comparison return 0
return

els

#If x is null and y is not null, y is greater
Write-debug "Comparison return -1"
return -


els

#If x is not null..
if ($y -eq $null

#...and y is null, x is greater
Write-debug "Comparison return 1
return

els
{
Write-debug "Call CompareTo
$x.CompareTo($y




#New-Delegate from 'flamework.net' (http://flamework.net/archives/3
$deleg = New-Delegate System.Comparison``1[System.Collections.Hashtable] $SBCompareH
[System.Reflection.MethodInfo] $Methode = GetMethodEx Array 'void sort[t](t[], system.comparison`1[t])
$MethodeGenerique = $Methode.MakeGenericMethod([System.Collections.Hashtable]
$
$MethodeGenerique.Invoke($null,@($T,$Deleg)
$


-------------------
 #4  
12-24-08, 03:01 PM
Josh Einstein
If you're using PowerShell 2.0 you will find it MUCH faster to use the
Add-Type cmdlet to create an IComparer<T> using C# syntax. If you're just
doing a couple of names like in your example then it's no biggie, but for
large datasets I've often found it impractical to use PowerShell syntax for
anything called O(n) times in a loop.

I'm gonna start blogging soon about ways that I've blended my C# and
PowerShell usage.

Josh Einstein

"BatchMan" <guest> wrote in message
news:8418
[..]
 #5  
12-25-08, 12:20 PM
BatchMan
Josh Einstein;919271 Wrote:
> If you're using PowerShell 2.0 you will find it MUCH faster to use the
> Add-Type cmdlet to create an IComparer<T> using C# syntax. If you're
> just
> doing a couple of names like in your example then it's no biggie, but
> for
> large datasets I've often found it impractical to use PowerShell syntax
> for
> anything called O(n) times in a loop.
>
> I'm gonna start blogging soon about ways that I've blended my C# and
> PowerShell usage.
>
> Josh Einstein
>

I agree completely.
It is not the best solution but it is a solution. It is a compromise.
Concerning the use of C# all ITs do not know it. This one is full
PowerShell.

See Also
'Custom IComparers in PowerShell (and Add-Type for v1)'
(http://huddledmasses.org/custom-icom...d-type-for-v1/)
Similar Threads
Reflection : GetEvent, GetMethod, Bind Method on Event

TValue oItem = (TValue)Activator.CreateInstance(typeof(TValue), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { m_nCleTemporaire...

Reflection.Emit: Emitting open constructed generic method

Hello Group, assume the following sample model: interface IGenericMethods { void DoSomething<T>(T obj); } class GenericMethods

getmethod in reflection

I want use getmethod to get an already known method and invoke it. The problem is, I don't know the parameters of this method, what I have is an Object[] array that contains...

Type.GetMethod with Generic method

I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic...

Type.GetMethod() with generic method parameters: a hen-or-egg problem

Hi there, I wonder how to get a MethodInfo using System.Reflection.Type::GetMethod when the method has a parameter which is a generic method parameter. Consider this...


All times are GMT. The time now is 06:08 PM. | Privacy Policy