C#交易程序中能否用for循环批量填充列表字段并动态取值?
批量处理交易程序中的刻度阈值字段(C#)
问题背景
我是C#新手,若概念或语法表述有误请见谅。我开发了一款交易程序,用List存储潜在交易及交易质量相关数据,其中一个List的元素包含.SHL20、.SHL30这类对应不同刻度阈值的字段,用来存储图表增量信息。
我需要实现两个核心需求:
- 批量填充这些刻度阈值字段
- 根据选择的阈值,用对应字段作为交易筛选条件
目前我靠手动重复代码填充字段(示例如下),也试过用switch-case选择字段,但代码冗余度极高。我想通过循环批量命名并赋值字段,写了一段伪代码,但不确定是否可行,同时想找更优雅的替代方案。
当前手动填充代码示例
IFVGopen[index].SHL20 = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + 20 > 0); IFVGopen[index].SHL30 = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + 30 > 0); IFVGopen[index].SHL40 = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + 40 > 0); IFVGopen[index].SHL50 = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + 50 > 0); IFVGopen[index].SHL60 = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + 60 > 0);
设想的循环伪代码
for(x = 10 ; x < 100 ; index = x + 10) { IFVGopen[index].x = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - index >= 0 && (p.price - tradeSide) * type + index + 10 > 0) }
同时希望能通过变量(比如40)直接获取IFVG[index].40这类对应字段的值,请问这种实现可行吗?有没有更优雅的替代方案?
解决方案
方案1:用Dictionary替代固定字段(推荐)
重构数据结构,把零散的.SHLXX字段换成Dictionary<int, int>类型的属性,这是最灵活且易维护的方案:
1. 修改数据类定义
public class TradeData // 替换成你实际的类名 { // 保留原有其他属性... public Dictionary<int, int> SHLValues { get; set; } = new Dictionary<int, int>(); }
2. 批量填充代码
// 定义需要处理的阈值列表,后续新增阈值只需添加到这里 var thresholds = new List<int> {20, 30, 40, 50, 60}; foreach(var threshold in thresholds) { IFVGopen[index].SHLValues[threshold] = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + threshold > 0); }
3. 根据阈值取值
int selectedThreshold = 40; int targetValue = IFVGopen[index].SHLValues[selectedThreshold];
方案2:反射实现批量赋值(临时方案)
如果暂时无法修改原有类结构,可以用反射批量操作字段,但反射性能损耗大、可读性差,仅作为过渡方案:
var thresholds = Enumerable.Range(2, 6).Select(x => x * 10); // 生成20、30、40、50、60 var targetObj = IFVGopen[index]; var objType = targetObj.GetType(); // 批量赋值 foreach(var threshold in thresholds) { var fieldName = $"SHL{threshold}"; var field = objType.GetField(fieldName); // 如果是属性则用GetProperty if(field != null) { int count = SwingHLopen.Count(p => p.type == -type && p.eqhlCount < 2 && (tradeSide - p.price) * type - 10 >= 0 && (p.price - tradeSide) * type + threshold > 0); field.SetValue(targetObj, count); } } // 根据阈值取值 int selectedThreshold = 40; var targetField = objType.GetField($"SHL{selectedThreshold}"); int targetValue = (int)targetField.GetValue(targetObj);
方案对比
- Dictionary方案:代码简洁、性能优异、扩展性强,后续新增阈值无需修改类结构,是首选方案。
- 反射方案:性能差、维护成本高,仅适合无法修改原有类结构的临时场景。
内容的提问来源于stack exchange,提问作者Mike Moeller justMike
相关产品推荐
相关产品推荐

