.net 托管WMI事件类不是事件类?
我正在使用下面的说明:创建托管事件类。以下是我编写的代码:.net 托管WMI事件类不是事件类?,.net,windows,wmi,managed,.net,Windows,Wmi,Managed,我正在使用下面的说明:创建托管事件类。以下是我编写的代码: [ManagementEntity] [InstrumentationClass(InstrumentationType.Event)] public class MyEvent { [ManagementKey] public string ID { get; set; } [ManagementEnumerator] static public IEnumerable<MyEvent> E
[ManagementEntity]
[InstrumentationClass(InstrumentationType.Event)]
public class MyEvent
{
[ManagementKey]
public string ID { get; set; }
[ManagementEnumerator]
static public IEnumerable<MyEvent> EnumerateInstances()
{
var e = new MyEvent() { ID = "9A3C1B7E-8F3E-4C54-8030-B0169DE922C6" };
return new MyEvent[] { e };
}
}
class Program
{
static void Main(string[] args)
{
var thisAssembly = typeof(Program).Assembly;
var wmi_installer = new AssemblyInstaller(thisAssembly, null);
wmi_installer.Install(null);
wmi_installer.Commit(null);
InstrumentationManager.RegisterAssembly(thisAssembly);
Console.Write("Press Enter...");
Console.ReadLine();
var e = new MyEvent() { ID = "A6144A9E-0667-415B-9903-220652AB7334" };
Instrumentation.Fire(e);
Console.Write("Press Enter...");
Console.ReadLine();
wmi_installer.Uninstall(null);
}
}
请注意,该类不是从\uuuuuExterinsectEvent
继承的,这很奇怪
我还可以从MyEvent运行select*和<代码>Instrumentation.Fire()
也不会返回任何错误。然而,当我试图使用“通知查询”订阅事件时,我得到
号码:0x80041059
设施:WMI
说明:类不是事件类
我做错了什么,有没有正确的方法来创建托管WMI事件?经过一番挖掘,我发现了发生了什么:显然在框架4中,引入了WMI类和属性的第二个“分支”,这干扰了经典类和属性。我在internet上找到的所有示例代码都是使用.NET2.0WMI支持编写的。我还没有找到一种使用.NET4类从_事件或_外部事件继承类的方法 微软在同一个名称空间中引入了两个不兼容的代码分支,这让人非常恼火,这两个分支不仅互不兼容,而且破坏了彼此的功能 无论如何,为了解决这个问题,我需要做的基本上是确保我的应用程序使用.NET 2代码:
- 完全摆脱wmi程序集中的
派生类。使用DefaultManagementInstaller
DefaultManagementProjectInstaller
- 使用
而不是Instrumentation.RegisterAssembly
InstrumentationManager.RegisterAssembly
- 在
上手动清理WMI命名空间,因为未使用.NET 2 api从命名空间中正确删除WMI类Uninstall()
- 使用.NET 2 api不可能将字段设置为
[key]
- 完全摆脱wmi程序集中的
派生类。使用DefaultManagementInstaller
DefaultManagementProjectInstaller
- 使用
而不是Instrumentation.RegisterAssembly
InstrumentationManager.RegisterAssembly
- 在
上手动清理WMI命名空间,因为未使用.NET 2 api从命名空间中正确删除WMI类Uninstall()
- 使用.NET 2 api不可能将字段设置为
[key]
[dynamic: ToInstance, provider("WmiTest,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
class MyEvent
{
[read, key] string ID;
};