Processor
クラスに単一のプロパティなどを持たせArchitecture
、そのコンストラクターでインスタンスを取得してみませんか?次に、のコンストラクターで管理オブジェクトから必要なデータを取得し、オブジェクトに多数のを作成できます。Cores
ManagementObject
Processor
Processor
PC
class PC
{
//I'd encapsulate these in a property rather than a public field
public Processor[] Processors;
public Motherboard Motherboard;
// Constructor
public PC()
{
Motherboard = new Motherboard();
}
// Method to get all info sequentially
public void GetAllInfo()
{
ManagementObject[] WMIData = DataRetriever.GetWMIData("Win32_Processor");
Processors = new Processor[WMIData.Length-1];
for (int i = 1; i < WMIData.Length; i++)
{
Processors[i-1] = new Processor(WMIData[i-1]); //assuming 0 based
}
Motherboard.GetInfo();
}
}
class Processor
{
public string Architecture;
public string Availability;
public UInt16 Cores;
public Processor(ManagementObject WMIData)
{
this.Architecture = (string)WMIData["Architecture"];
this.Availability = (string)WMIData["Availability"];
this.Cores = (UInt16)WMIData["NumberOfCores"];
}
}
パフォーマンスが心配な場合は、パブリックフィールドをプロパティの背後に隠してから、必要に応じて怠惰に電話をかける必要があります。明らかに、これは、必要なときにデータをロードすること(アクセス時に遅延が発生する可能性があります)とすべてのデータをプリロードすること(開始時に遅延を意味する可能性がありますが、必要なときに迅速になります)の間のトレードオフです。望ましい結果は、常にすべてのデータが必要かどうか、およびそれがどのように使用されるかによって決まります。
WMIDataオブジェクトをProcessorクラスに格納し、プロパティにアクセスするときに値を読み取ることができます。遅いビットがどこにあるかによって異なります。
class Processor
{
private ManagementObject WMIData;
// obviously you might want to cache this value once it has been retrieved once
public string Architecture{get{return (string)WMIData["Architecture"];}}
public string Availability {get{return (string)WMIData["Availability"];}}
public UInt16 Cores{get{return (UInt16)WMIData["NumberOfCores"]}}
public Processor(ManagementObject WMIData)
{
this.WMIData = WMIData;
}
}
編集
複数のWMIクエリが必要な場合は、各WMI呼び出しの結果をオブジェクトに渡して、オブジェクトからデータを取得できるようにするか(遅くなるように聞こえます)、それらの呼び出しを実行できるように十分なデータをオブジェクトに渡します。必要な場合:
class HardDrive
{
private int index;
private ManagmentObject physicalMediaInfo;
private ManagementObject smartDataInfo;
// choose one of these constructors. this one lets you delay all the WMI calls till you need to do them
public HardDrive(int index)
{
this.index=index;
}
//this one means you have to make the calls in advance
public HardDrive(ManagmentObject physicalMediaInfo,ManagementObject smartDataInfo)
{
this.physicalMediaInfo=physicalMediaInfo;
this.smartDataInfo=smartDataInfo;
}
private ManagementObject PhysicalMediaInfo
{
get
{
if(physicalMediaInfo==null)
{
ManagementObject[] WMIData = DataRetriever.GetWMIData("Win32_PhysicalMedia");
physicalMediaInfo=WMIData[index];
}
return physicalMediaInfo;
}
}
private ManagementObject SmartDataInfo
{
get
{
if(smartDataInfo==null)
{
ManagementObject[] WMIData = DataRetriever.GetWMIData("ATAPI_SmartData");
smartDataInfo=WMIData[index];
}
return smartDataInfo;
}
}
//property for getting the details of the hard disk
//uses the private property to ensure that the management object for the is only loaded when its needed
public int Sectors{get{return (int)PhysicalMediaInfo["Sectors"]};};
//Same for the smart data.
public int SomeSmartData{get{return (int)SmartDataInfo["SomeSmartData"]};};
}
このアプローチでは、必要なプロパティとして各api呼び出しのみを実行でき、使用されているプロパティの数に関係なく、一度だけ実行されるようになります。コンストラクターを組み合わせて、渡される管理オブジェクトをnullにすることができます。次に、ルックアップに使用されるインデックスと、コンストラクターで渡されなかったManagementObjectインスタンスを提供できますが、一部のManagementObjectsは自由に渡すことができます。それらが利用可能または安価である場合、これはプリロードされる可能性があります...
編集2
更新の処理に関しては、次回データにアクセスするときにAPiからの最新情報が要求されるように更新する必要があると仮定すると、次のようにするだけで済みます。
public void Refresh()
{
this.physicalMediaInfo=null;
this.smartDataInfo=null;
}
これは、次にSectorsが呼び出されたときに、既存のWMIObjectが置き換えられるため、WMIDataから値が再照会されることを意味します。