如何从 powershell 调用 .Net class 库中的 public 函数

How do I call a public function in a .Net class library from powershell

我有 .Net c# class 库 (C:\path1\path2\Proj1.Web\bin\Proj1.Web.dll)。对象浏览器显示我要调用的public函数如下

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

我正在使用

在 powershell 中加载程序集
$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

由于该函数不是 static(或 VB.Net 术语中的 Shared),您需要创建 class 的实例来调用该方法在:

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