How to call a C# DLL with custom type input from Powershell
08:26 15 Oct 2024

Let us say I have a C# DLL which contains a class AdditionLibrary as below

namespace AdditionLibrary
{
    public static class AdditionOperation
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        public struct AddNumbers
        {
            public int a;
            public int b;
        }
        public static int AddNum(AddNumbers addNum)
        {
            return addNum.a + addNum.b;
        }
    }
}

I want to call Add & AddNum methods of this class from powershell. I can easily call Add method since it is POD data type (int) like below from powershell.

Add-Type -Path "AdditionLibrary.dll"
$result = [AdditionLibrary.AdditionOperation]::Add(100,200)

How to call AddNum method which requires custom type AddNumbers?

Can it be possible without embedding code inside powershell like below & call through invoke-expression ?

Add-Type -TypeDefinition $code -Language CSharp 

I do not know how to create instance of AddNumbers in powershell and pass that as a parameter to C# DLL.

c# powershell