使用 Powershell 执行 C# 代码 [英] Execute C# code using Powershell
问题描述
我可以编写 C# 脚本代码并使用 PowerShell 执行它吗?
Can I write a C# script code and execute it using PowerShell?
我想使用 Notepad++ 编写 C# 脚本并使用 PowerShell 执行代码,而不是使用 Visual Studio 提供的命令行工具运行 C#.
Instead of running C# using command line tools provided by Visual Studio, I want to write C# script using Notepad++ and execute the code using PowerShell.
推荐答案
在 PowerShell 中使用 C# 代码
Using C# Code in PowerShell
客户喜欢脚本语言,因为它允许他们编写自定义代码而无需运行编译器或将新的可执行文件复制到他们的生产机器,这通常需要比部署脚本文件甚至执行脚本文件更复杂的批准过程命令外壳中的命令.
"Customer like scripting languages as it allows them to write custom code without a need to run a compiler or to copy new executables to their production machines which usually requires a more complex approval process than deploying a script file or even to execute the commands within a command shell.
因此,如果现有的 C# 代码可以在 PowerShell 中重用,而无需将其实现为 Cmdlet,那就太好了."
So it would be great if the existing C# code could be reused inside PowerShell without a need to implement it as Cmdlet."
$Assem = (
...add referenced assemblies here...
)
$Source = @"
...add C# source code here...
"@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
博客文章中提供了完整示例
Full example provided in the blog article
这篇关于使用 Powershell 执行 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!