我知道 Powershell 可以调用 .NET 代码,可能看起来像这样
PS> [Reflection.Assembly]::LoadFile(($ScriptDir + ".\SharpSvn-x64\SharpSvn.dll"))
PS> $SvnClient = New-Object SharpSvn.SvnClient
而且我知道 C# 有out
参数的地方,Powershell 有[ref]
参数,可能看起来像这样:
PS> $info = $null
PS> $SvnClient.GetInfo($repo.local, ([ref]$info))
True
PS> $info
(...long output snipped...)
NodeKind : Directory
Revision : 16298
Uri : http://server/path/to/remoterepo
FullPath : C:\path\to\localrepo
(...long output snipped...)
而且我知道在 C# 中您可以重载函数,就像 SharpSvn 库为其SvnClient.Update() 方法所做的那样:
Update(ICollection(String))
- 将指定路径递归更新到最新(HEAD)修订版Update(String)
- 递归更新指定路径到最新(HEAD)修订Update(ICollection(String), SvnUpdateArgs)
- 将指定路径更新到指定修订版Update(ICollection(String), SvnUpdateResult)
- 将指定路径递归更新到最新(HEAD)修订版Update(String, SvnUpdateArgs)
- 递归更新指定路径Update(String, SvnUpdateResult)
- 递归更新指定路径到最新(HEAD)修订Update(ICollection(String), SvnUpdateArgs, SvnUpdateResult)
- 将指定路径更新到指定修订版Update(String, SvnUpdateArgs, SvnUpdateResult)
- 递归更新指定路径到最新(HEAD)修订
但是,如果我们想把所有这些放在一起怎么办?例如,如果我想调用第 6 个版本Update()
,即接受 String 和 SvnUpdateResult 的版本,其中 SvnUpdateResult 是 C# out 对象?我的第一直觉是尝试这样的事情:
PS> $repopath = "C:\path\to\localrepo"
PS> $update = $null
PS> $svnclient.update($repopath, [ref]$update)
Multiple ambiguous overloads found for "Update" and the argument count: "2".
At line:1 char:18
+ $svnclient.update <<<< ($repopath, [ref]$update)
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
好的,也许我必须提出论点?
PS> $svnclient.update([string]$repopath, [ref][SharpSvn.SvnUpdateResult]$update)
Multiple ambiguous overloads found for "Update" and the argument count: "2".
At line:1 char:18
+ $svnclient.update <<<< ([string]$repopath, [ref][SharpSvn.SvnUpdateResult]$update)
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
但这似乎也不起作用。我尝试过的其他事情:
- 铸造
$update
为[SharpSvn.SvnUpdateResult][ref]
- 也就是说,颠倒我铸造它的顺序。这会导致一个错误,指出:“[ref] 只能是类型转换序列中的最终类型。” - 在使用它之前投射
$update
到: . 这导致了我在上面遇到的相同的“多个不明确的重载”错误SharpSvn.SvnUpdateResult
$update = [SharpSvn.SvnUpdateResult]$null
- 在使用它之前投射
$update
到: . 这会导致错误:“无法将“System.Management.Automation.PSReference”类型的“System.Management.Automation.PSReference”值转换为“SharpSvn.SvnUpdateResult”类型。ref
$update = [ref]$null
似乎将其投射两次是问题所在 - 最后一次投射只是覆盖了第一次投射,它们不会相互补充。这是怎么回事?有没有办法两次施放东西?有没有其他方法可以解决这个问题?
提前感谢您的帮助。