在 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 方法的最简单方法是什么?
How do you call a generic static method of a custom class in Powershell?
Given the following class:
public class Sample
{
public static string MyMethod<T>( string anArgument )
{
return string.Format( "Generic type is {0} with argument {1}", typeof(T), anArgument );
}
}
And this is compiled into an assembly 'Classes.dll' and loaded into PowerShell like this:
Add-Type -Path "Classes.dll"
What's the easiest way to call the MyMethod method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 @Athari 所说,调用 MyMethod 的最简单方法是使用 MakeGenericMethod。由于他实际上没有展示如何做到这一点,因此这里是一个经过验证的工作代码示例:
带有输出
The easiest way to call MyMethod is, as @Athari says, to use MakeGenericMethod. Since he doesn't actually show how to do that, here is a verified working code sample:
with output
可以调用泛型方法,参考帖子 在 PowerShell 中调用非泛型类的泛型方法。
这并不简单,您需要使用
MakeGenericMethod
函数。如果方法没有重写,这很简单,如果有重写,那就会变得更难。以防万一,从那里复制粘贴代码:
You can call generic methods, refer to the post Invoking Generic Methods on Non-Generic Classes in PowerShell.
This is not straightforward, you need to use
MakeGenericMethod
function. It is pretty simple if method doesn't have overrides, it gets harder if it does.Just in case, copy-pasted code from there:
这是 PowerShell 的限制,无法直接在 PowerShell V1 或 V2 中完成(据我所知)。
顺便说一句,您的通用方法并不是真正通用的。不应该是这样吗:
如果您拥有此代码并希望从 PowerShell 使用它,请避免泛型方法或编写非泛型 C# 包装器方法。
This is a limitation of PowerShell and can't be done directly in PowerShell V1 or V2 AFAIK.
BTW your generic method isn't really generic. Shouldn't it be:
If you own this code and want to use it from PowerShell, avoid generic methods or write a non-generic C# wrapper method.
好消息是 PowerShell v3 在绑定到通用方法(并具体化它们?)方面要好得多,并且您通常不需要做任何特殊的事情,只需像调用普通方法一样调用它即可。我无法指定现在适用的所有标准,但根据我的经验,即使在 PowerShell v4 中,通用参数的某些情况仍然需要解决方法(可能是存在或重载或类似的东西)。
同样,我有时也无法将通用参数传递给方法......例如传递
Func
参数。对我来说,一种比 MakeGenericMethod 或其他方法简单得多的解决方法是直接在我的脚本中放置一个快速的 C# 包装类,并让 C# 整理所有通用映射...
以下是这种方法的一个示例,它包装了
Enumerable.Zip
方法。在此示例中,我的 C# 类根本不是通用的,但严格来说这并不是必需的。这会产生:
我确信有更好的 PowerShell 方法来“压缩”两个数组,但你明白了。我在这里回避的真正挑战是为
Zip
提供硬编码(在 C# 类中)的第三个参数,因此我不必弄清楚如何传入该Func
(也许还有一种 PowerShell 方法可以做到这一点?)。The good news is PowerShell v3 is much better at binding to generic methods (and reifying them?) and you often don't have to do anything special but call it as you would a normal method. I can't specify all of the criteria for which this now works, but in my experience certain situations with generic parameters still require workarounds even in PowerShell v4 (maybe its the existence or overloads or something similar).
Similarly I also sometimes have trouble passing a generic parameter to a method ... for instance passing a
Func<T1, T2, TResult>
parameter.One work-around that to me is much simpler than MakeGenericMethod or other approaches is to just put a quick C# wrapper class directly in my script, and let C# sort out all the generic mapping ...
Here is an example of this approach that wraps the
Enumerable.Zip
method. In this example my c# class isn't generic at all but that isn't strictly speaking necessary.This produces:
I'm sure there are better PowerShell ways to "Zip" two arrays but you get the idea. The real challenge that I dodged here was having a hard-coded (in the C# class) 3rd parameter to
Zip
so I didn't have to figure out how to pass in thatFunc<T1, T2, TResult>
(Maybe there is a PowerShell way to do that as well?).如果没有名称冲突,快速方法:
Fast way, if there are no name conflicts: