このプログラムは、WMIデータを一連のクラス(コンピューターのハードウェア要素ごとに1つのクラス)に編成し、特定のハードウェア要素が複数存在する場合、各クラスは複数回初期化されます。
コードのこのセクションをいくつかの関数呼び出しに変換するための優れた方法はありますか?CreateComponent(ref object dataClass, params string[] WMIClasses);
私は、WMIデータの一時ストアを使用して各インスタンスを追加するためのforループを作成するのではなく、コンピューターコンポーネントを初期化するための方針に沿って何かを考えていました。
// These temporary stores fetch WMI data as ManagementObjects
// Most cases will only need one WMI class.
ManagementObject[] WMIDataTemp1;
ManagementObject[] WMIDataTemp2;
// Fetch data as ManagementObjects
WMIDataTemp1 = DataRetriever.GetWMIData("Win32_Processor");
// Loop though each ManagementObject and add a new device for each instance
foreach (ManagementObject Object in WMIDataTemp1)
{
this.Processors.Add(new Processor(Object));
}
WMIDataTemp1 = DataRetriever.GetWMIData("Win32_Baseboard");
WMIDataTemp2 = DataRetriever.GetWMIData("Win32_MotherboardDevice");
for (int i = 0; i < WMIDataTemp1.Length; i++)
{
this.Motherboards.Add(new Motherboard(WMIDataTemp1[i], WMIDataTemp2[i]));
}
// And so on for all the other bits of hardware...