3

I call C# functions from PowerShell this way:

// C# code
public void Foo(string param1, int param2, Dictionary<string, string> param3)

# PowerShell code:  
Add-Type -Path $pathToDll

$myClass= New-Object -TypeName MyClass
$dic= New-Object 'System.Collections.Generic.Dictionary[String,String]'
$dic.Add("Key1","Val1")
$dic.Add("Key2","Val2")

$myClass.Foo("Test1", 1, $dic)

But what if I want to call function with signature that has params in it:

public void Foo(string param1, int param2, params string[] param3)

How to call this function from powershell?

If I call it like this:

 $myClass.Foo("Test1", 1, "foo1", "foo2", "foo3")

It shows an error: Cannot find an overload for "Foo" and the argument count: "5".

1
  • 1
    To clarify the answer of Martin : your method signature tells you the method takes 3 parameters, a string, a int and an array of strings. So you'll have to either create an array of strings, store that in a variable and yse it as the 3rd parameter OR you use the way Martin described (I prefer the last one) Commented Feb 17, 2018 at 11:16

1 Answer 1

5

I think you have to call it like this:

$myClass.Foo("Test1", 1, @("foo1", "foo2", "foo3"))

Basically you have to create an array for the third parameter using @()

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

1 Comment

That's called an explicit array.

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.