0

I'm currently trying to execute methods from a C# Class library .dll with a powershell script.

This is the one and only class of my library:

namespace DLLCalculator
{
public class Calculator
{

    public static void addNumbers(int num1, int num2)
    {

        MessageBox.Show((num1 + num2).ToString());

    }



    public void helloWorld()
    {

        MessageBox.Show("Hello World!");

    }


    }
}

This is the code of my powershell:

[Reflection.Assembly]::LoadFile("C:\Users\Admin\source\repos\DLLCalculator\DLLCalculator\bin\Debug\DLLCalculator.dll")
[DLLCalculator.Calculator]::addNumbers(10,5)
$Object = new-object DLLCalculator.Calculator
$Object.helloWorld()

This part of the powershell script here works perfectly fine and I get a messagebox with the desired result:

[DLLCalculator.Calculator]::addNumbers(10,5)

This here however does not work for some reason:

$Object = new-object DLLCalculator.Calculator
$Object.helloWorld()

How I see it it should work considering that the powershell script creates an object in order to execute the non static method.

I have also tried the command using the expression "-TypeName" from this stackoverflow thread but unfortunately it did not work either.

This is the error I'm getting:

Method invocation failed because [DLLCalculator.Calculator] does not contain a method named 'helloWorld'.
At C:\Users\Admin\source\repos\PowershellStarter\PowershellStarter\bin\Debug\test.ps1:4 char:1
+ $Object.helloWorld()
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

As seen above the function DOES exist but for some reason the powershell can't seem to find it for some odd reason. What could be causing the problem?

5
  • @iRon Which "static property"? And: your link is broken. Commented Jul 28, 2022 at 11:01
  • @iRon unfortunattely the link does not work. Commented Jul 28, 2022 at 11:12
  • @iRon I am kind of confused by your question and think that you might have misinterpreted my question. My class is not written in powershell but in C#. That class exists within a class library (.dll file) My goal is now calling the static and non static method from the .DLL file (which is writen in C#) with the powershell console. Commented Jul 28, 2022 at 11:48
  • 2
    Works for me when using Add-Type, I haven't tried with a whole assembly. Perhaps it's loading the wrong version of the library, perhaps a previous version was already loaded Commented Jul 28, 2022 at 11:57
  • @Charlieface This was it. I was loading the debug version but my IDE was compiling the release version. lol The code is actually correct and works fine if the path is correct Commented Jul 28, 2022 at 12:02

0

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.