管理データの公開
データ管理では、管理のためにクラスを公開する必要がある場合もあります。
.NET Framework クラスを定義し、実装し、管理可能なクラスとしてマークします。この処理は、クラスに InstrumentationClass(InstrumentationType.Instance) 属性を使用するか、または Instance クラスからクラスを派生させることによって実行できます。これにより、マネージ コード クラスの WMI スキーマが自動的に生成されます。クラスのインスタンスは、すべてのプロパティの値が割り当てられた WMI インスタンスとして公開されます。
実装クラスはデザイン時 (コンパイル時) に定義されます。この定義は、アプリケーションの果たす役割の前半部分に相当します。実行時には、アプリケーションは実際のデータを提供する必要があります。マネージ コードの開発者は、WMI 対応の構造にデータをフォーマットするからといって、新しい方法を学習する必要はありません。WMI 実装クラスを定義するときと同じマネージ クラスを使用して、マネージ アプリケーションはクラスのインスタンスをインスタンス化し、フィールドの値を指定することによって、データを提供します。WMI を通じて公開されるデータは、マネージ クラスのフィールドとプロパティから直接読み取られます。
マネージ コードの開発者が WMI を通じて実装を公開するために必要とする基本要件は次のとおりです。
サポートされているいずれかの言語を使用して、実装を記述するマネージ クラスを定義します。
このマネージ クラスは、WMI メソッドやクラス定義のための WMI 文法を理解していなくても、WMI クラスに割り当てられます。
実行時には、クラスのインスタンスを作成し、フィールドの値を指定し、インスタンスを発行することによって情報が WMI に公開されます。
管理実装のインスタンス クラスを作成し、このクラスのインスタンスを WMI に公開する方法を次のコード例に示します。
using System;
using System.Management;
using System.Configuration.Install;
using System.Management.Instrumentation;
// This example demonstrate how to define a management instrumentation
// class and how to publish an instance of this class to WMI.
// Specify which namespace the data should be published into
[assembly:Instrumented("root/default")]
// Let the system know InstallUtil.exe utility will be run against
// this assembly
[System.ComponentModel.RunInstaller(true)]
public class MyInstaller : DefaultManagementProjectInstaller {}
// Define a management instrumentation class
[InstrumentationClass(InstrumentationType.Instance)]
public class InstanceClass {
public string Name;
public int Number;
}
public class Sample_InstanceProvider {
public static int Main(string[] args) {
InstanceClass instClass = new InstanceClass();
instClass. Name = "Hello";
instClass. Number = 888;
// Publish this instance to WMI
Instrumentation.Publish(instClass);
Console.WriteLine("Instance now visible through WMI");
Console.ReadLine();
Instrumentation.Revoke(instClass); //optional
return 0;
}
}
[Visual Basic]
Imports System
Imports System.Management
Imports System.Configuration.Install
Imports System.Management.Instrumentation
' This example demonstrate how to define a management instrumentation
' class and how to publish an instance of this class to WMI.
' Specify which namespace the data should be published into
<assembly: Instrumented("Root/Default")>
' Let the system know InstallUtil.exe utility will be run against
' this assembly
<System.ComponentModel.RunInstaller(True)> _
Public Class MyInstaller
Inherits DefaultManagementProjectInstaller
End Class
' Create a management instrumentation instance class
<InstrumentationClass(InstrumentationType.Instance)> _
Public Class InstanceClass
Public Name As String
Public Number As Integer
End Class
Public Class Sample_InstanceProvider
Overloads Public Shared Function Main(args() As String) As Integer
Dim instClass As New InstanceClass()
instClass.Name = "Hello"
instClass.Number = 888
' Publish this instance to WMI
System.Management.Instrumentation.Instrumentation.Publish(instClass)
Console.WriteLine("Instance now visible through WMI")
Console.ReadLine()
System.Management.Instrumentation.Instrumentation.Revoke(instClass)
Return 0
End Function
End Class
参照
System.Management を使用する .NET Framework アプリケーションの実装 | CLI と WMI におけるクラスとマップ | 管理イベントの公開 | 継承 | 実装されたアプリケーションのスキーマ登録