在PowerShell中调用静态通用LINQ扩展方法[英] Calling static generic LINQ extension method in PowerShell

本文是小编为大家收集整理的关于在PowerShell中调用静态通用LINQ扩展方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

一个简单的符号可以在PowerShell中调用许多LINQ方法:

[int[]] $numbers = 1..10000
[Linq.Enumerable]::Sum($numbers)

在呼叫中包括lambda甚至是相对简单的事情:

[Func[int,int]] $delegate = { $n = $args[0]; if ($n % 3) { $n } else { -$n } }
[Linq.Enumerable]::Sum($numbers, $delegate)

我想知道的是如何从PowerShell中调用 enemic linq方法:这甚至有可能吗? 我找到了这个问题这似乎表明可以,但是我没有确定如何将该信息应用于LINQ. (另外,这是旧信息的事实,很有可能有了PS版本5,就有一种更干净的方法.)

那么,如何在PowerShell中正确调用[Linq.Enumerable]::Cast<T>(...)或[Linq.Enumerable]::OfType<T>(...)?

2017.05.10更新

好,因此,基于@Mathias评论,让我们坚持MakeGenericMethod.在C#中,此咒语有效:

var ofTypeForString = typeof(System.Linq.Enumerable).GetMethod("OfType").MakeGenericMethod(typeof(string));
var stuff = new object[] { 1.2, "abc", "def" };
var results = ofTypeForString.Invoke(null, new[] { stuff });

我仍然缺少的作品是如何将typeof(System.Linq.Enumerable)转换为PowerShell.我认为其中至少有一个应该起作用,但它们都返回null:

[System.Type]::GetType("System.Linq.Enumerable")
[System.Type]::GetType("Linq.Enumerable")
[System.Type]::GetType("Enumerable")

我敢肯定我缺少简单的东西;建议?

推荐答案

是的,当然,Petseral和Ejohnson的评论都是正确的;出于某种原因,我只是精神障碍.因此,这是对可能感兴趣的人的完整解决方案:

$stringType = "".GetType() # set to your target type
$ofTypeForString =
        [Linq.Enumerable].GetMethod("OfType").MakeGenericMethod($stringType)
$stuff = @("12345", 12, "def")
# The last comma below wraps the array arg $stuff within another array
$ofTypeForString.Invoke($null, (,$stuff)) 

本文地址:https://www.itbaoku.cn/post/1556989.html

问题描述

One can call many LINQ methods in PowerShell with this simple notation:

[int[]] $numbers = 1..10000
[Linq.Enumerable]::Sum($numbers)

It is even a relatively simple matter to include a lambda in a call:

[Func[int,int]] $delegate = { $n = $args[0]; if ($n % 3) { $n } else { -$n } }
[Linq.Enumerable]::Sum($numbers, $delegate)

What I am wondering, though, is how to call a generic LINQ method from PowerShell: is it even possible? I found this SO question that seems to indicate one can, but I have not determined how to apply that info to LINQ. (Plus, the fact that that is old information, it is quite possible that with PS version 5 there is a cleaner way to do it.)

So how could one call [Linq.Enumerable]::Cast<T>(...) or [Linq.Enumerable]::OfType<T>(...) properly in PowerShell?

2017.05.10 Update

OK, so based on @Mathias comment, let's stick with MakeGenericMethod. In C#, this incantation works:

var ofTypeForString = typeof(System.Linq.Enumerable).GetMethod("OfType").MakeGenericMethod(typeof(string));
var stuff = new object[] { 1.2, "abc", "def" };
var results = ofTypeForString.Invoke(null, new[] { stuff });

The piece I am still missing is how to translate typeof(System.Linq.Enumerable) to PowerShell. I thought that at least one of these should work but they all return null:

[System.Type]::GetType("System.Linq.Enumerable")
[System.Type]::GetType("Linq.Enumerable")
[System.Type]::GetType("Enumerable")

I am sure I am missing something simple; suggestions?

推荐答案

Yes, both PetSerAl and ejohnson's comments are correct, of course; I just had a mental block for some reason. So here is the complete solution for those who may be interested:

$stringType = "".GetType() # set to your target type
$ofTypeForString =
        [Linq.Enumerable].GetMethod("OfType").MakeGenericMethod($stringType)
$stuff = @("12345", 12, "def")
# The last comma below wraps the array arg $stuff within another array
$ofTypeForString.Invoke($null, (,$stuff))