0
votes

I have .Net c# class library (C:\path1\path2\Proj1.Web\bin\Proj1.Web.dll). The object browser shows the public function I want to call as follows

public void RunMe()
Member of Proj1.Web.Services.RunCustomServices

I am loading the assembly in powershell using

$asm=[Reflection.Assembly]::LoadFile("C:\path1\path2\Proj1.Web\bin\Proj1.Web.dll")
[Proj1.Web.Services.RunCustomServices]::RunMe()

Method invocation failed because [Proj1.Web.Services.RunCustomServices] doesn't contain a method named 'Runme'. At line:1 char:1 + [Proj1.Web.Services.RunCustomServices]::RunMe() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFoundPS C:\users\home> [Proj1.Web.Services.RunCustomServices]::RunMe() Method invocation failed because [Proj1.Web.Services.RunCustomServices] doesn't contain a method named 'RunMe'. At line:1 char:1 + [Proj1.Web.Services.RunCustomServices]::RunMe() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

1
Step 1: look up Add-Type instead of using reflection directly.Eris
@Eris, care to explain why would that help in your opinion?Andrew Savinykh
Nate, if your method is not static, which it is not, you need an instance of the class to call the method on. The syntax you are trying to use only works for static methods.Andrew Savinykh

1 Answers

1
votes

Since the function is not static (or Shared in VB.Net terminology), you'll need to create an instance of the class to call the method on:

$RCSInstance = New-Object Proj1.Web.Services.RunCustomServices
$RCSInstance.RunMe()