如何使用Add

编程入门 行业动态 更新时间:2024-10-25 17:21:41
如何使用Add-Type在System.Windows.Forms命名空间中添加C#代码?(How can I Use Add-Type to add C# code with System.Windows.Forms namespace?)

如何使用Add-Type在System.Windows.Forms命名空间中添加C#代码? 我试过这个命令:

Add-Type -TypeDefinition @" using System; using System.Windows.Forms; namespace testnamespace { public static class testclass { public static string messagebox() { MessageBox.Show("test") return "test"; } } } "@

但我得到一些错误,如:

名称空间'System.Windows'中不存在类型或命名空间名称'Forms'(您是否缺少程序集引用?)

我只想在PowerShell中使用ONLY C#代码。 请不要给我替代。

How can I use Add-Type to add C# code with System.Windows.Forms namespace? I tried this command:

Add-Type -TypeDefinition @" using System; using System.Windows.Forms; namespace testnamespace { public static class testclass { public static string messagebox() { MessageBox.Show("test") return "test"; } } } "@

but I'm getting some error like:

The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

I just want to use ONLY C# code in PowerShell. Please don't give me alternative.

最满意答案

使用ReferencedAssemblies参数添加对包含程序集( System.Windows.Forms.dll文件)的ReferencedAssemblies :

Add-Type -TypeDefinition @" using System; using System.Windows.Forms; namespace TestNamespace { public static class TestClass { public static string MessageBox() { MessageBox.Show("test"); return "test"; } } } "@ -ReferencedAssemblies System.Windows.Forms

Add a reference to the containing assembly (the System.Windows.Forms.dll file), using the ReferencedAssemblies parameter:

Add-Type -TypeDefinition @" using System; using System.Windows.Forms; namespace TestNamespace { public static class TestClass { public static string MessageBox() { MessageBox.Show("test"); return "test"; } } } "@ -ReferencedAssemblies System.Windows.Forms

更多推荐

本文发布于:2023-07-18 11:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!