Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将其缩短为函数_C#_Function_For Loop_Paramarray - Fatal编程技术网

C# 将其缩短为函数

C# 将其缩短为函数,c#,function,for-loop,paramarray,C#,Function,For Loop,Paramarray,此程序将WMI数据组织到一组类中—计算机中每个硬件元素对应一个类—如果存在多个特定硬件元素,则每个类将被初始化多次 有没有一种很好的方法将这段代码转换成几个函数调用?我想到了一些类似于CreateComponent的东西(ref-object-dataClass,params-string[]WMIClasses)初始化计算机组件,而不是使用WMI数据的临时存储并创建for循环来添加每个实例 // These temporary stores fetch WMI data as

此程序将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...
//这些临时存储将WMI数据作为管理对象获取
//大多数情况下只需要一个WMI类。
ManagementObject[]WMIDataTemp1;
ManagementObject[]WMIDataTemp2;
//将数据作为管理对象获取
wmidatemp1=DataRetriever.GetWMIData(“Win32_处理器”);
//循环遍历每个ManagementObject并为每个实例添加新设备
foreach(WMIDataTemp1中的ManagementObject对象)
{
this.Processors.Add(新处理器(对象));
}
wmidatemp1=DataRetriever.GetWMIData(“Win32_基板”);
wmidatemp2=DataRetriever.GetWMIData(“Win32_主板设备”);
for(int i=0;i
你试过LINQ吗

Processors = DataRetriever.GetWMIData("Win32_Processor").Select(x => new Processor(x)).ToList();

这可能更适合codereview.stackexchange.comI,谢谢。我会先看看是否有人能给出答案。嗯,不太管用,因为一些硬件类需要不止一个WMI类来初始化。另外,我的目标框架是2.0,我认为它不包含LINQ。