我是PowerShell的新手,尝试在PowerShell控制台中运行内联C#代码。但是在成功运行后开始出错。 这是我的PowerShell脚本:
$Source = @"
namespace TestNamespace
{
public static class MyClass
{
public static string GetMessage()
{
return "Text from C#";
}
}
}
"@
Add-Type -TypeDefinition $Source -Language CSharp
[TestNamespace.MyClass]::GetMessage()
这是我得到的错误:
Exception calling "GetMessage" with "0" argument(s): "An item with the same key has already been added."
它成功运行了2/3次,但在此之后它开始显示此错误,即使我尝试关闭PowerShell控制台并重新打开。任何人都可以帮助我,这个脚本/场景可能有什么问题。
有一件事,可能是有趣的,这是正在运作的原始剧本。然后我从' string'更改了GetMessage()方法的返回类型。到'无效'然后问题就开始了。现在即使这个原始脚本也不起作用,显示相同的错误消息。