0

This program organises WMI data into a set of classes - one class for each hardware element in a computer - and each class is initialised multiple times if more than one of a particular hardware element exists.

Is there a nice neat way of turning this section of code into a few function calls? I was thinking of something along the lines of CreateComponent(ref object dataClass, params string[] WMIClasses); to initialise a computer component rather than using temporary stores for WMI data and making a for loop to add each instance.

        // 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...
Chris Watts
  • 6,197
  • 7
  • 49
  • 98

1 Answers1

0

Have you tried LINQ?

Processors = DataRetriever.GetWMIData("Win32_Processor").Select(x => new Processor(x)).ToList();
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720