如何为多个结构体编写统一的Computer参数读写代码?
如何用单段代码统一处理不同结构体中Computer的读写操作
场景与代码背景
我定义了4个结构体:Computer、Home、Car、Ship,并声明了以下数组:
- 可容纳5台
Computer的数组; - 可容纳20个
Home的数组,每个Home包含一个Computer; - 可容纳20个
Car的数组,每个Car包含一个Computer; - 可容纳20个
Ship的数组,每个Ship包含一个Computer。
对应的代码实现:
struct Computer { public string pcName; public string motherBoardName; public string graphicCardName; public int memorySize; public int numberOfDrivers; } struct Home { public Computer pc; // 其他家庭相关参数(int, byte, string, double等) } struct Car { public Computer pc; // 其他车辆相关参数(int, byte, string, double等) } struct Ship { public Computer pc; // 其他船舶相关参数(int, byte, string, double等) } Computer[] pc = new Computer[5]; // 5台计算机 Home[] home = new Home[20]; // 最多20个家庭 Car[] car = new Car[20]; // 最多20辆车 Ship[] ship = new Ship[20]; // 最多20艘船
核心需求
希望通过面板上的输入框、Set/Get按钮和单选控件,对pc数组中的Computer,以及Home、Car、Ship数组中各自包含的Computer进行参数读写。能否用单段代码实现所有场景下的Computer参数设置与读取,而非编写多组重复的读写函数?
可行方案:泛型+委托统一处理
因为结构体是值类型,修改嵌套的Computer时需要注意值拷贝特性,我们可以通过泛型方法配合委托,统一提取和更新目标Computer实例,彻底避免重复代码。
1. 统一读取逻辑
编写通用读取方法,接收一个获取Computer的委托,将参数填充到面板输入框:
private void LoadComputerParams(Func<Computer> getComputer) { Computer targetPc = getComputer(); // 将参数赋值给面板控件 txtPcName.Text = targetPc.pcName; txtMotherBoard.Text = targetPc.motherBoardName; txtGraphicCard.Text = targetPc.graphicCardName; numMemorySize.Value = targetPc.memorySize; numDrivers.Value = targetPc.numberOfDrivers; }
2. 统一写入逻辑
编写通用写入方法,接收一个更新Computer的委托,从面板获取值并更新目标实例:
private void SaveComputerParams(Action<Computer> updateComputer) { Computer updatedPc = new Computer { pcName = txtPcName.Text, motherBoardName = txtMotherBoard.Text, graphicCardName = txtGraphicCard.Text, memorySize = (int)numMemorySize.Value, numberOfDrivers = (int)numDrivers.Value }; updateComputer(updatedPc); }
3. 结合单选控件调用统一方法
根据用户选择的操作对象,传入对应的委托即可完成读写:
// Get按钮点击事件示例 private void btnGet_Click(object sender, EventArgs e) { int selectedIndex = // 获取用户选择的数组索引(如从下拉框/输入框读取) if (radioPc.Checked) { LoadComputerParams(() => pc[selectedIndex]); } else if (radioHome.Checked) { LoadComputerParams(() => home[selectedIndex].pc); } else if (radioCar.Checked) { LoadComputerParams(() => car[selectedIndex].pc); } else if (radioShip.Checked) { LoadComputerParams(() => ship[selectedIndex].pc); } } // Set按钮点击事件示例 private void btnSet_Click(object sender, EventArgs e) { int selectedIndex = // 获取用户选择的数组索引 if (radioPc.Checked) { SaveComputerParams(updated => pc[selectedIndex] = updated); } else if (radioHome.Checked) { SaveComputerParams(updated => { Home temp = home[selectedIndex]; temp.pc = updated; home[selectedIndex] = temp; }); } else if (radioCar.Checked) { SaveComputerParams(updated => { Car temp = car[selectedIndex]; temp.pc = updated; car[selectedIndex] = temp; }); } else if (radioShip.Checked) { SaveComputerParams(updated => { Ship temp = ship[selectedIndex]; temp.pc = updated; ship[selectedIndex] = temp; }); } }
方案优势
- 读写逻辑仅需编写一次,完全避免重复代码;
- 委托可灵活适配不同类型的数组,无论是直接的
Computer数组,还是嵌套Computer的结构体数组; - 适配结构体值类型特性:通过临时变量拷贝-修改-存回的逻辑,正确处理嵌套结构体的更新。
可选优化:接口约束(允许修改结构体定义时使用)
如果可以修改结构体定义,让Home、Car、Ship实现统一接口,代码可进一步简化:
interface IHasComputer { Computer Pc { get; set; } } // 修改结构体实现接口 struct Home : IHasComputer { public Computer Pc { get; set; } // 其他参数... } struct Car : IHasComputer { public Computer Pc { get; set; } // 其他参数... } struct Ship : IHasComputer { public Computer Pc { get; set; } // 其他参数... }
新增泛型工具方法处理所有IHasComputer类型的数组:
private void SaveToHasComputerArray<T>(T[] array, int index, Computer updatedPc) where T : struct, IHasComputer { T temp = array[index]; temp.Pc = updatedPc; array[index] = temp; }
调用时可简化为:
else if (radioHome.Checked) { SaveComputerParams(updated => SaveToHasComputerArray(home, selectedIndex, updated)); }
这样能进一步减少重复的结构体拷贝代码,提升可维护性。
内容的提问来源于stack exchange,提问作者Programmer man
相关产品推荐
相关产品推荐

