.net - 如何从 Powershell 调用使用 C# 输出参数的重载 .NET 函数?

标签 .net powershell reference overloading

我知道 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() method 所做的那样。 :

  1. Update(ICollection(String)) - 递归地将指定路径更新为最新 (HEAD) 版本
  2. Update(String) - 递归地将指定路径更新为最新(HEAD)版本
  3. Update(ICollection(String), SvnUpdateArgs) - 将指定路径更新为指定版本
  4. Update(ICollection(String), SvnUpdateResult) - 递归地将指定路径更新为最新 (HEAD) 版本
  5. Update(String, SvnUpdateArgs) - 递归更新指定路径
  6. Update(String, SvnUpdateResult) - 递归地将指定路径更新为最新 (HEAD) 版本
  7. Update(ICollection(String), SvnUpdateArgs, SvnUpdateResult) - 将指定路径更新为指定版本
  8. Update(String, SvnUpdateArgs, SvnUpdateResult) - 递归地将指定路径更新为最新 (HEAD) 版本

但是,如果我们想将所有这些放在一起怎么办?比如说,如果我想调用 Update() 的第 6 个版本,即采用 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 转换为 ref:$update = [ref]$null。这会导致错误:“无法将“System.Management.Automation.PSReference”类型的“System.Management.Automation.PSReference”值转换为“SharpSvn.SvnUpdateResult”类型。”

看来两次转换是问题所在——最后一次转换只是覆盖了第一次转换,它们并不互相补充。这是正在发生的事情吗?有没有办法将某个东西转换两次?还有其他方法可以解决这个问题吗?

预先感谢您的帮助。

最佳答案

这可能不是一个理想的解决方案,但您可以使用 Add-Type 并创建一个更适合 PowerShell 的类型来包装 Update 方法。

Add-Type -TypeDefinition "
public class SvnClientEx
{
   public static SvnUpdateResult Update(SvnClient client, string path)
   {
       SvnUpdateResult result; 
       client.Update(path, out result);
       return result; 
   } 
}
"

$result = [SvnClient]::Update($svnClient, $repopath)

关于.net - 如何从 Powershell 调用使用 C# 输出参数的重载 .NET 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958955/

相关文章:

c++ - C++ 中的对象变量保存值,而不是对象引用

c# - 将 linq-to-sql 表达式的一部分分解为一个单独的函数

.net - Powershell 卸载模块...完全

html - Powershell 更改 RTF 文档并另存为 RTF 文档

在 R 中滚动你自己的链表/树?

php - 错误消息 "Strict standards: Only variables should be passed by reference"

python - 按值比较 lambda 表达式指针

C# NetworkStream 数据丢失

python - powershell tts 命令旁白使用 python 更改

powershell - 使用 Powershell 将证书导入 Tomcat 中的 keystore