![](/img/trans.png)
[英]How to pass a powershell script to Windows PowerShell Host in C#?
[英]Pass interface type to c# in Powershell script?
我有一个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<int, List<int>>
对象,该对象(出于某种原因;我不知道为什么)与IDictionary<int, IList<int>>
不兼容。
如何在Powershell中创建兼容类型?
采用:
$myArg= New-Object 'Collections.Generic.Dictionary[int, Collections.Generic.IList[int]]'
$list = New-Object 'Collections.Generic.List[int]'
简而言之,这将字典的TValue
更改为C#库期望的IList<int>
。
但是,列表本身并未声明为接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.