该程序将WMI数据组织成一组类 - 计算机中每个硬件元素的一个类 - 如果存在多个特定硬件元素,则每个类都会被初始化多次。将其缩短为一个函数
将这部分代码转换为几个函数调用是否有一个很好的方法?我正在考虑按照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...
这可能更适合codereview.stackexchange.com – ramblinjan 2012-03-12 22:20:06
我不知道这一点,谢谢。我会先看看有没有人提出答案。 – CJxD 2012-03-12 22:42:43