如何使用 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";
        }
    }
}
"@ 

但我收到一些错误,例如:

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

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

使用 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