Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 如何改进从列表映射到ModelRequest的代码_C#_List_Mapping - Fatal编程技术网

C# 如何改进从列表映射到ModelRequest的代码

C# 如何改进从列表映射到ModelRequest的代码,c#,list,mapping,C#,List,Mapping,您好,这是从SensorID字段和SensorValue创建的列表到请求模型的映射,其中SensorID的所有值都报告为属性 我这样做: 公共列表SensorsStatus{get;set;} 公共阶级地位 { [必需] 公共字符串SensorId{get;set;} 公共字符串SensorValue{get;set;} } 以及模型请求: 公共类NotificheDiStatoModel { 公共int?qrStatus{get;set;} 公共bool?启用{get;set;} public

您好,这是从SensorID字段和SensorValue创建的列表到请求模型的映射,其中SensorID的所有值都报告为属性

我这样做:

公共列表SensorsStatus{get;set;} 公共阶级地位 { [必需] 公共字符串SensorId{get;set;} 公共字符串SensorValue{get;set;} } 以及模型请求:

公共类NotificheDiStatoModel { 公共int?qrStatus{get;set;} 公共bool?启用{get;set;} public int?clessStatus{get;set;} 公共bool?clessEnabled{get;set;} 公共int?magStatus{get;set;} 公共bool?magEnabled{get;set;} 公共int?puncherStatus{get;set;} 公共bool?punchrenabled{get;set;} 公共int?bleStatus{get;set;} 公共bool?blenabled{get;set;} } 目前有效的映射我是这样做的,但我想改进代码

var listasensori=req.MessageBody.SensorsStatus; NotifichEstatomodel outputreq=新NotifichEstatomodel; 列表传感器中的每个无功传感器 { 如果传感器E.SensorId.EqualBleStatus outputreq.bleStatus=转换为32传感器E.SensorValue; 如果传感器E.SensorId.EqualsBlEnabled已启用 outputreq.blenabled=Convert.tobooleansore.SensorValue; 如果传感器E.SensorId.Equalsclessstatus outputreq.clessStatus=转换为32传感器E.SensorValue; 如果传感器E.SensorId.Equalsclessenabled outputreq.clessEnabled=转换为ooleansore.SensorValue; 如果传感器E.SensorId.Equalsqrstatus outputreq.qrStatus=转换为32传感器E.SensorValue; 如果传感器E.SensorId.EqualSqr启用 outputreq.qrEnabled=转换为.TOBOOLEANSERVE.SensorValue; 如果传感器E.SensorId.Equalsmagstatus outputreq.magStatus=转换为32传感器E.SensorValue; 如果传感器E.SensorId.Equals已成像 outputreq.magEnabled=转换为OOLEANSENSOR E.SensorValue; 如果传感器E.SensorId.EqualsPunchrStatus outputreq.PunchrStatus=转换为32传感器E.SensorValue; 如果传感器E.SensorId.Equals已穿孔 outputreq.puncherEnabled=转换为ooleansore.SensorValue; };
提前谢谢。

可以这样简化。只需将您在Equals中签入的所有字符串添加到列表中。下面的例子

NotifichEstatomodel outputreq=新NotifichEstatomodel; var传感器=新列表; var acceptedSensorIds=新列表{blestatus,BLESENABLED,CLESSTATUS,CLESENABLED,qrstatus}; 传感器中的foreach无功传感器 { AcceptedSensorId.ForEachid=> { 如果sensore.SensorId.equalId,则为StringComparison.OrdinalIgnoreCase { var propertyInfo=outputreq.GetType.GetPropertyid; 属性info.SetValueoutputreq,Convert.ToBooleansensore.SensorValue; } }; } 我会将您的SensorId设置为枚举类型而不是字符串类型:

公共枚举SensorIdEnum{ 布莱斯状态, 布莱恩德, 克莱斯状态, ... } 您的SensorStatus类将成为:

公共阶级地位 { [必需] public SensorIdEnum SensorId{get;set;} 公共字符串SensorValue{get;set;} } 这将降低打字错误的风险,并使文档和扩展更容易

然后,我将使用泛型从一开始就对SensorValue属性权限强制执行类型

公共阶级地位 { [必需] public SensorIdEnum SensorId{get;set;} 公共T传感器值{get;set;} } 通过这种方式,您可以使数据转换成为SensorStatus自身在其构造函数中的责任

您可以使outputreq基本上成为一个您可以传递的字典,而不是一个您不必完全使用的成熟NotificheDiStatoModel类:

var listasensori=req.MessageBody.SensorsStatus; var outputreq=新字典; 列表传感器中的每个无功传感器 { outputreq[sensore.SensorId]=sensore.SensorValue; };
@JohnathanBarclay可能不止一个,因为它在整个循环中都是相同的outputreq。但它并不总是转换为布尔值。