4

I have C# constructor

class A {
   public A (name="",version=""){
     //do something
   }
}

The corresponding DLL is imported in Powershell. I want to create the A object by passing named parameters.

$a = New-Object ABC.XYZ.A -ArgumentList @()  //pass named params

I couldn't find the doc/example to create object with constructor which takes optional named parameters [there are around 20 params].

1 Answer 1

4

I do not think it is possible, but you can work around it by deriving from the class with 20 parameters. See the following

$Source = @"
namespace DontCare
{
    /**/
    public class TheCrazyClassWith20parametersCtor
    {
        public TheCrazyClassWith20parametersCtor(/* 20 named parameters here*/)
        {}
    }

    public class MyWrapper : TheCrazyClassWith20parametersCtor
    {
        public MyWrapper(int param1, string param2)
        : base(
            /* use named parameters here*/
        )
        {} 
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

New-Object -TypeName DontCare.MyWrapper -ArgumentList 42,"Hi!"

HTH

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

Comments

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.