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?
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