返回列表 发帖

[问题求助] PS中嵌入C#代码后,能执行但是报错

下面的代码,能执行成功, 但是总是有如下错误, 求大佬解惑, 谢谢


Add-Type : Cannot add type. The type name 'ClassLibrary1.Class1' already exists.
At line:15 char:1
+ Add-Type -TypeDefinition $code
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (ClassLibrary1.Class1:String) [Add-Type], Excep
   tion
    + FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
$code = @'
namespace ClassLibrary1
{
public class Class1
{
public static void SayHello()
{
System.Console.WriteLine("hello world");
}
}
}
'@
Add-Type -TypeDefinition $code
[ClassLibrary1.Class1]::SayHello()COPY

在一个powershell session中, 同一个Class 只能添加一次.
Class1 改成Class2 就行了
微信:flashercs
QQ:49908356

TOP

回复 2# flashercs


    谢谢大佬提醒, 有时报错, 有时不报错, 先不管了

TOP

返回列表