我有以下 .csx 脚本:
#r "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Management.dll"
using System.Management;
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
当我使用它运行时,dotnet script
我收到此错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Management.MTAHelper.IsNoContextMTA()
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..ctor(String path)
at System.Management.ManagementClass..ctor(String path)
at Submission#0.<<Initialize>>d__0.MoveNext() in <..path..>\script.csx:line 3
--- End of stack trace from previous location ---
at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in <...path...>\Temp\tmpBFB\Dotnet.Script.Core\ScriptRunner.cs:line 110
如果我在 Visual Studio 中编译以下代码,一切正常:
using System.Management;
class Script {
static void Main() {
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
}
}
为什么 ?我怎样才能让我的 .csx 脚本工作?