【问题标题】:Pass interface type to c# in Powershell script?在Powershell脚本中将接口类型传递给c#?
【发布时间】:2016-07-01 08:53:25
【问题描述】:

我有一个 C# 库,我正在尝试编写脚本,其中最重要的调用之一具有以下方法签名:

DoThing(IDictionary<int, IList<int>> dict)

我需要编写一个构造要传入的对象的 powershell 脚本。我已经编写了 powershell 来构造相同的东西,但是使用了具体的类:

$myArg= New-Object 'Collections.Generic.Dictionary[int, Collections.Generic.List[int]]'
$list = New-Object 'Collections.Generic.List[int]'

$myArg.Add(7, $list)

[PowershellReflectionTest.ReflectionTester]::DoThing($myArg)

然而,这实际上是在创建一个Dictionary&lt;int, List&lt;int&gt;&gt; 对象,它(出于某种原因;我不知道为什么)与IDictionary&lt;int, IList&lt;int&gt;&gt; 不兼容。

如何在 powershell 中创建兼容类型?

【问题讨论】:

    标签: c# .net powershell reflection scripting


    【解决方案1】:

    用途:

    $myArg= New-Object 'Collections.Generic.Dictionary[int, Collections.Generic.IList[int]]'
    $list = New-Object 'Collections.Generic.List[int]'
    

    简而言之,这会将 Dictionary 的 TValue 更改为您的 C# 库所期望的 IList&lt;int&gt;

    但列表本身并未声明为接口。

    【讨论】:

    • 哇。我不敢相信我没想过要尝试。我假设因为在 powershell 中没有强大的左侧输入,所以它是不够的。
    猜你喜欢
    • 2015-06-07
    • 2022-01-08
    • 2021-12-05
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多