【问题标题】:How can I Use Add-Type to add C# code with System.Windows.Forms namespace?如何使用 Add-Type 添加带有 System.Windows.Forms 命名空间的 C# 代码?
【发布时间】:2016-12-18 12:11:32
【问题描述】:

如何使用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# 代码。请不要给我其他选择。

【问题讨论】:

  • 使用菜单项目:添加新项目:Windows 窗体。它会自动在一个新类中设置所有内容。

标签: c# .net powershell powershell-2.0


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 2010-12-05
    • 2020-05-08
    • 2016-11-17
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多