Csharp/C#教程:使用PowerShell调用Visual Studio方法分享


使用PowerShell调用Visual Studio方法

我正在尝试编写一个powershell脚本来调用位于我的Visual Studio(2010)解决方案中的方法。 调用的语法是什么,例如,类似的方法

public void CreateData(string s, int i) 

您可以将类库项目编译为dll,然后使用powershell中的reflection加载dll。 这是一个例子:

c#代码:

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

powershell代码:

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

此代码假定您的类名为“MyClass”。 您可能必须先在powershell中运行set-executionpolicy RemoteSigned。

您需要构建C#类并使用类似InvokeCSharp的方法来调用CreateData方法

请参阅链接以获得一个好例子https://vincenth.net/blog/archive/2009/10/27/call-inline-c-from-powershell-with-invokecsharp.aspx

我会创建一个这样的程序:

 class Program { static void Main(string[] args) { if(args.Length > 1) CreateData(args[0], int.Parse(args[1])); } public static void CreateData(string s, int i) { File.WriteAllText(@"C:data.txt", string.Format("This is some data: s = {0} i = {1}", s, i)); } } 

然后编译程序后我有可执行文件“ConsoleApp.exe”

我启动了以下的PowerShell脚本:

 & "Path_Of_The_ProgramConsoleApp.exe" "MyText" 100 

它成功地将数据写入我的文件中。

上述就是C#学习教程:使用PowerShell调用Visual Studio方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1008321.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐