Question:
I created a simple C# dll and registered it using RegAsm.exe
. One very simple method is invoked without parameters and returns a number, here is a simplified version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace AVL_test { interface ITestClass { int GetNumber(); } [ComVisible(true)] public class TestClass: ITestClass { public TestClass() { } public int GetNumber() { return 10; } } } |
I need to invoke that method from Powershell, so I added the type
1 2 |
Add-Type -Path "C:\myPath\AVL_test.dll" |
It seems to be loaded because if I [AVL_test.TestClass]
I get this output
IsPublic IsSerial Name BaseType
——– ——– —- ——–
True False TestClass System.Object
But if I try to invoke GetNumber()
by typing[AVL_test.TestClass]::GetNumber()
I get this error
Method invocation failed because [AVL_test.TestClass] does not contain a method named ‘GetNumber’.
Am I doing something wrong?
Answer:
Your method should be static
or you need to create an instance of that type (TestClass).
Last can be done using
1 2 |
New-Object -TypeName |
Or in your specific case:
1 2 3 |
$test = New-Object -TypeName AVL_Test.TestClass $test.GetNumber() |