如何通过命令行在 powershell 中使用 Add-Type 以执行 VB.NET 或 C#(或 JScript.net)代码? [英] How I can use Add-Type in powershell through command line in order to execute VB.NET or C# (or JScript.net) code?

查看:57
本文介绍了如何通过命令行在 powershell 中使用 Add-Type 以执行 VB.NET 或 C#(或 JScript.net)代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于 添加类型

所以我试图通过命令行添加类型:

So I'm trying to add type through command line:

>powershell Add-Type -TypeDefinition "public class T{public static int Add(int a, int b){return (a + b);}}" -Language CSharp

>powershell Add-Type -TypeDefinition "@"""public class T{public static int Add(int a, int b){return (a + b);}}"""@" -Language CSharp

>powershell Add-Type -TypeDefinition "@"""public class T{public static int Add(int a, int b){return (a + b);}}"""@" -Language CSharp

>powershell Add-Type -TypeDefinition @"public class T{public static int Add(int a, int b){return (a + b);}}"@ -Language CSharp

但是无论我尝试执行什么都会返回错误.是否可以通过cmd控制台调用powershell来添加类型?然后执行添加的方法?

But whatever I try the execution returns an error. Is it possible to add type by calling the powershell through the cmd console? And then execute the added methods?

推荐答案

这就是为什么你不能做你正在做的事情的原因,文档特别说明了这一点.

Here's why you can't do what you are doing and the docs specifically show this.

这种标点符号称为 这里是字符串.,它有特定用途.

This punctuation is called a Here String. and it serves a specific purpose.

如果您在真正的 PowerShell 编辑器(ISE、VSCode)中执行此操作,请注意将所有这些放在一行中会产生语法错误.

If you did this in a real PowerShell Editor (ISE, VSCode), note that putting all this on one line generates a syntax error.

这里是正确的字符串

$Source = @"
public class T
{
    public static int Add(int a, int b)
    {
        return (a + b);
    }
}
"@

这不正确(将突出显示语法错误)...

This is not proper (will show syntax error highlighting)...

$Source = @"public class T{public static int Add(int a, int b){return (a + b);}}"@

...因为这些'@""@'必须在单独的行上并且完全左对齐,并且没有任何东西可以跟在同一行的第一个之后或在同一行的第二个之前

... because these '@""@' must be on separate lines and fully left aligned and nothing can follow the first one on the same line or be in front of the second one on the same line

这不显示语法错误突出显示:

This does not show syntax error highlighting:

$Source = @"
public class T{public static int Add(int a, int b){return (a + b);}}
"@

...这可以在 PowerShell 控制台主机中完成,但不能在调用 powereshell.exe 的 cmd.exe 中完成.

... this can be done in the PowerShell console host but not cmd.exe calling powereshell.exe.

在这种情况下,控制台主机不会像编辑器一样显示语法错误.

The consolehost will not show syntax errors as well as the editors, if at all in this case.

任何时候您在控制台主机中输入需要 CRLF 等的代码时,都必须以这种方式输入.最后,在 cmd.exe 中执行此操作永远不会显示 PowerShell 语法错误,除非您尝试运行它.

Anytime you are typing code in the consolehost that requires a CRLF or the like, you have to type it that way. Lastly, doing this in cmd.exe would never show PowerShell syntax errors, until you try and run it.

这完全是为了完全理解 PowerShell 语法和标点符号.

This is all about fully understanding PowerShell syntax and punctuation.

完整指南到 PowerShell 标点符号

这篇关于如何通过命令行在 powershell 中使用 Add-Type 以执行 VB.NET 或 C#(或 JScript.net)代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆