.net 对ManagementObjectCollection.Count的引用引发无效的类异常

.net 对ManagementObjectCollection.Count的引用引发无效的类异常,.net,wmi,.net,Wmi,在下面的代码段中,注释行将抛出System.Management.ManagementException,其中包含消息Invalid Class。但是,在迭代集合后引用Count属性可以正常工作 此代码来自一个单元测试,该测试正在运行,但随后开始失败。我还不能确定发生了什么变化 ManagementScope scope = new ManagementScope(@"root\mypath"); scope.Options.Impersonation = ImpersonationLevel.

在下面的代码段中,注释行将抛出System.Management.ManagementException,其中包含消息Invalid Class。但是,在迭代集合后引用Count属性可以正常工作

此代码来自一个单元测试,该测试正在运行,但随后开始失败。我还不能确定发生了什么变化

ManagementScope scope = new ManagementScope(@"root\mypath");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
ObjectQuery query = new ObjectQuery("SELECT * FROM myclass");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection result = searcher.Get();
//int count = result.Count // throws exception
foreach (ManagementObject mo in result)
{
    Console.WriteLine(mo.Path);
}
int count = result.Count // no problem

有什么想法吗?

你能告诉我你使用的是哪个类和路径吗?这是一个自定义的路径和类。你能详细分享你的路径和类信息吗?我不想显示实际的文本,但知道它不会帮助任何人诊断问题。不喜欢mypath和myclass?使用任何其他随机字符。