Подтвердить что ты не робот

Вызов универсального статического метода в PowerShell

Как вы называете общий статический метод пользовательского класса в Powershell?

Учитывая следующий класс:

public class Sample
{
    public static string MyMethod<T>( string anArgument )
    {
        return string.Format( "Generic type is {0} with argument {1}", typeof(T), anArgument );
    }
}

И это скомпилировано в сборку "Classes.dll" и загружается в PowerShell следующим образом:

Add-Type -Path "Classes.dll"

Какой самый простой способ вызвать метод MyMethod?

4b9b3361

Ответ 1

Вы можете вызвать общие методы, обратитесь к сообщению Вызов общих методов для не общих классов в PowerShell.

Это не просто, вам нужно использовать функцию MakeGenericMethod. Это довольно просто, если у метода нет переопределений, становится все сложнее, если он делает.

На всякий случай, скопированный код оттуда:

## Invoke-GenericMethod.ps1 
## Invoke a generic method on a non-generic type: 
## 
## Usage: 
## 
##   ## Load the DLL that contains our class
##   [Reflection.Assembly]::LoadFile("c:\temp\GenericClass.dll")
##
##   ## Invoke a generic method on a non-generic instance
##   $nonGenericClass = New-Object NonGenericClass
##   Invoke-GenericMethod $nonGenericClass GenericMethod String "How are you?"
##
##   ## Including one with multiple arguments
##   Invoke-GenericMethod $nonGenericClass GenericMethod String ("How are you?",5)
##
##   ## Ivoke a generic static method on a type
##   Invoke-GenericMethod ([NonGenericClass]) GenericStaticMethod String "How are you?"
## 

param(
    $instance = $(throw "Please provide an instance on which to invoke the generic method"),
    [string] $methodName = $(throw "Please provide a method name to invoke"),
    [string[]] $typeParameters = $(throw "Please specify the type parameters"),
    [object[]] $methodParameters = $(throw "Please specify the method parameters")
    ) 

## Determine if the types in $set1 match the types in $set2, replacing generic
## parameters in $set1 with the types in $genericTypes
function ParameterTypesMatch([type[]] $set1, [type[]] $set2, [type[]] $genericTypes)
{
    $typeReplacementIndex = 0
    $currentTypeIndex = 0

    ## Exit if the set lengths are different
    if($set1.Count -ne $set2.Count)
    {
        return $false
    }

    ## Go through each of the types in the first set
    foreach($type in $set1)
    {
        ## If it is a generic parameter, then replace it with a type from
        ## the $genericTypes list
        if($type.IsGenericParameter)
        {
            $type = $genericTypes[$typeReplacementIndex]
            $typeReplacementIndex++
        }

        ## Check that the current type (i.e.: the original type, or replacement
        ## generic type) matches the type from $set2
        if($type -ne $set2[$currentTypeIndex])
        {
            return $false
        }
        $currentTypeIndex++
    }

    return $true
}

## Convert the type parameters into actual types
[type[]] $typedParameters = $typeParameters

## Determine the type that we will call the generic method on. Initially, assume
## that it is actually a type itself.
$type = $instance

## If it is not, then it is a real object, and we can call its GetType() method
if($instance -isnot "Type")
{
    $type = $instance.GetType()
}

## Search for the method that:
##    - has the same name
##    - is public
##    - is a generic method
##    - has the same parameter types
foreach($method in $type.GetMethods())
{
    # Write-Host $method.Name
    if(($method.Name -eq $methodName) -and
    ($method.IsPublic) -and
    ($method.IsGenericMethod))
    {
        $parameterTypes = @($method.GetParameters() | % { $_.ParameterType })
        $methodParameterTypes = @($methodParameters | % { $_.GetType() })
        if(ParameterTypesMatch $parameterTypes $methodParameterTypes $typedParameters)
        {
            ## Create a closed representation of it
            $newMethod = $method.MakeGenericMethod($typedParameters)

            ## Invoke the method
            $newMethod.Invoke($instance, $methodParameters)

            return
        }
    }
}

## Return an error if we couldn't find that method
throw "Could not find method $methodName"

Ответ 2

Самый простой способ вызвать MyMethod - это, как говорит @Athari, использовать MakeGenericMethod. Поскольку он на самом деле не показывает, как это сделать, вот пример проверенного рабочего кода:

$obj = New-Object Sample

$obj.GetType().GetMethod("MyMethod").MakeGenericMethod([String]).Invoke($obj, "Test Message")
$obj.GetType().GetMethod("MyMethod").MakeGenericMethod([Double]).Invoke($obj, "Test Message")

с выходом

Generic type is System.String with argument Test Message
Generic type is System.Double with argument Test Message

Ответ 3

Это ограничение PowerShell и не может быть выполнено непосредственно в PowerShell V1 или V2 AFAIK.

Кстати, ваш общий метод не является общим. Не должно быть:

public static string MyMethod<T>(T anArgument)
{ 
   return string.Format( "Generic type is {0} with argument {1}", 
                         typeof(T), anArgument.ToString()); 
} 

Если вы владеете этим кодом и хотите использовать его из PowerShell, избегайте общих методов или пишите не общий метод оболочки С#.

Ответ 4

Хорошей новостью является то, что PowerShell v3 намного лучше привязывается к универсальным методам (и обновляет их?), и вам часто не нужно делать ничего особенного, но называть его, как обычный метод. Я не могу указать все критерии, для которых это работает сейчас, но по моему опыту некоторые ситуации с общими параметрами по-прежнему требуют обходных решений даже в PowerShell v4 (возможно, это существование или перегрузка или что-то подобное).

Аналогично, мне также иногда приходится переносить общий параметр в метод... например, передавая параметр Func<T1, T2, TResult>.

Одна работа, которая для меня намного проще, чем MakeGenericMethod или другие подходы, - просто поместить быстрый класс оболочки С# непосредственно в мой script, и пусть С# сортирует все общее отображение...

Вот пример такого подхода, который обертывает метод Enumerable.Zip. В этом примере мой класс С# не является общим вообще, но это не является строго необходимым.

Add-Type @'
using System.Linq;
public class Zipper
{
    public static object[] Zip(object[] first, object[] second)
    {
        return first.Zip(second, (f,s) => new { f , s}).ToArray();
    }
}
'@
$a = 1..4;
[string[]]$b = "a","b","c","d";
[Zipper]::Zip($a, $b);

Это дает:

 f s
 - -
 1 a
 2 b
 3 c
 4 d

Я уверен, что лучше использовать PowerShell для "Zip" двух массивов, но вы получаете эту идею. Настоящая проблема, с которой я уклонилась здесь, заключалась в том, что был 3-мерный параметр с жестким кодом (в классе С#) до Zip, поэтому мне не приходилось выяснять, как пройти в этом Func<T1, T2, TResult> (возможно, есть способ PowerShell сделайте это тоже?).

Ответ 5

Быстрый способ, если конфликтов имен нет:

[Sample]::"MyMethod"("arg")