我有一个.NET类,有许多可选参数,比如:
void Method(int one, int two, int three = 0, int four = 0, int five = 0);
five ,不列出参数 three four ?
five
three
four
在C#,我可以做:
instance.Method(1, 2, five: 5);
PowerShell有类似的语法吗?
基本上,您需要计算命名参数的相应参数索引,然后用 [type]::Missing 代替要忽略的可选参数 MethodInfo.Invoke() :
[type]::Missing
MethodInfo.Invoke()
$method = $instance.GetType().GetMethod("Method") # assuming Method has no additional overloads $params = @(1, 2, [type]::Missing, [type]::Missing, 5) $method.Invoke($instance, $params)
https://mcpmag.com/articles/2015/11/04/net-members-in-powershell.aspx
希望这有帮助