1

I have a C# library that I'm trying to script against where one of the most important calls has the following method signature:

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

I need to write a powershell script that constructs an object to be passed in. I have written powershell to construct the same thing, but with concrete classes:

$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)

However, this is effectively creating a Dictionary<int, List<int>> object, which is (for some reason; I don't know why) incompatible with IDictionary<int, IList<int>>.

How can I create a compatible type in powershell?

1 Answer 1

1

Use:

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

In short, this changes the Dictionary's TValue to be an IList<int> as expected by your C# library.

The list itself is not declared as an interface however.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. I can't believe I didn't think to try that. I assumed because there's no strong left-side typing in powershell that it wouldn't be enough or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.