PowerShell 7中如何使用[Func[int[], int]]委托类型?
PowerShell中[Func<int[], int>]的正确使用语法
在PowerShell里,[Func[int, int[]]]这类泛型委托能正常调用,但使用[Func[int[], int]]时会报错:
InvalidOperation: Unable to find type [Func].
相关命令输出参考
- 执行
[Func[int, int]] | Format-Table -Hide,输出:True True Func2 System.MulticastDelegate` - 执行
[Func[int, int[]]] | Format-Table -Hide,输出:True True Func2 System.MulticastDelegate` - 执行
[Func[int[], int]] | Format-Table -Hide,输出:InvalidOperation: Unable to find type [Func].
问题原因与解决方法
这是PowerShell解析泛型类型时的语法歧义导致的:当泛型参数是数组类型时,PowerShell会错误地把[]识别为整个Func类型的数组后缀,而非泛型参数的一部分。
正确写法需要用括号包裹数组类型的泛型参数,明确解析范围:
[Func[[int[]], int]]
执行上述语法就能正常获取到对应的泛型委托类型,不会再抛出错误。
内容的提问来源于stack exchange,提问作者Methodox Charles Zhang
相关产品推荐
相关产品推荐

