调用使用PowerShell的Visual Studio的方法方法、PowerShell、Visual、Studio

2023-09-03 07:57:24 作者:官方小可爱

我试图写一个PowerShell脚本调用多数民众赞成设在我的Visual Studio(2010年)解决方案的方法。 什么是语法要求,例如,像

I am trying to write a powershell script to call a method that's located in my Visual Studio (2010) solution. What is the syntax for calling, for example, a method like

public void CreateData(string s, int i)

推荐答案

您可以编译类库项目到一个DLL,然后在PowerShell中使用反射加载DLL。下面是一个例子:

You can compile your class library project into a dll then load the dll using reflection in powershell. Here is an example:

C#code:

public class MyClass {
    public void CreateData(string s, int i) {
        // your logic here
    }
}

PowerShell的code:

powershell code:

$lib = [Reflection.Assembly]::LoadFile("C:\path\to\MyClass.dll")
$obj = new-object MyClass
$result = $obj.CreateData("s_value",5)

这code假设你的类被称为MyClass的。您可能需要运行设置executionpolicy RemoteSigned就是在PowerShell中第一位。

This code assumes that your class is called "MyClass". You may have to run set-executionpolicy RemoteSigned in powershell first.

 
精彩推荐
图片推荐