protected virtual void CreateFieldDefinitions(string path, object target, IDictionary <object, object> values) { string propKey = ""; foreach (var prop in target.GetType().GetProperties()) { try { propKey = prop.Name.ToString(); if (values.ContainsKey(propKey)) { var propValue = values[propKey]; if (propValue != null) { if (prop.PropertyType == typeof(string)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_char, DataTypeName = "String", Group = path, Key = $"{path}.{propKey}", Size = 0, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(decimal)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_float, DataTypeName = "Single", Group = path, Key = $"{path}.{propKey}", Size = 4, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(float)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_float, DataTypeName = "Single", Group = path, Key = $"{path}.{propKey}", Size = 4, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(double)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_double, DataTypeName = "Double", Group = path, Key = $"{path}.{propKey}", Size = 8, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(int)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_int, DataTypeName = "Int32", Group = path, Key = $"{path}.{propKey}", Size = 4, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(Int32[])) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_int, DataTypeName = "Int32[]", Group = path, Key = $"{path}.{propKey}", Size = 4, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(bool)) { SessionFieldDefinitions.Add( new TelemetrySessionFieldDefinition() { Name = propKey, DataType = irsdk_VarType.irsdk_bool, DataTypeName = "Boolean", Group = path, Key = $"{path}.{propKey}", Size = 1, IsCalculated = true, Unit = GetUnit(propKey) }); } else if (prop.PropertyType == typeof(IList <ResultsPosition>)) { // skip this, handled } else if (prop.PropertyType == typeof(IList <Driver>)) { // skip this, handled } else if (prop.PropertyType == typeof(IList <ResultsFastestLap>)) { // skip this, handled } else if (prop.PropertyType == typeof(WeekendOptions)) { // skip this, handled } else { UnhandledDataTypes.Add(new UnhandledDataTypeError() { Field = prop.Name, Value = values[prop.Name.ToString()].ToString(), DataType = prop.PropertyType.Name }); } } } else { Console.WriteLine($"No value for {propKey}"); } } catch (FormatException) { FormatExceptions.Add(new FormatExceptionError() { Field = prop.Name, Value = values[prop.Name.ToString()].ToString() }); } catch (Exception ex) { string errorValue = "MISSING"; if (values.ContainsKey("")) { errorValue = values[prop.Name.ToString()].ToString(); } Console.WriteLine($"{propKey}: [{errorValue}] {ex.ToString()}"); } } }
protected virtual void SetValues(object target, IDictionary <object, object> values) { string propKey = ""; foreach (var prop in target.GetType().GetProperties()) { try { propKey = prop.Name.ToString(); if (values.ContainsKey(propKey)) { var propValue = values[propKey]; if (propValue != null) { propValue = Sanitize(propKey, propValue); if (prop.PropertyType == typeof(string)) { prop.SetValue(target, propValue); } else if (prop.PropertyType == typeof(decimal)) { prop.SetValue(target, decimal.Parse(propValue.ToString())); } else if (prop.PropertyType == typeof(int)) { prop.SetValue(target, int.Parse(propValue.ToString())); } else if (prop.PropertyType == typeof(Int32[])) { var propArrayValues = propValue.ToString().Split(','); Int32[] arrayValues = new Int32[propArrayValues.Length]; for (int i = 0; i < propArrayValues.Length; i++) { arrayValues[i] = int.Parse(propArrayValues[i]); } prop.SetValue(target, arrayValues, null); } else if (prop.PropertyType == typeof(bool)) { var boolValue = (propValue.ToString() == "1"); prop.SetValue(target, boolValue); } else if (prop.PropertyType == typeof(IList <ResultsPosition>)) { // skip this, handled } else if (prop.PropertyType == typeof(IList <Driver>)) { // skip this, handled } else if (prop.PropertyType == typeof(IList <ResultsFastestLap>)) { // skip this, handled } else if (prop.PropertyType == typeof(WeekendOptions)) { // skip this, handled } else if (prop.PropertyType == typeof(Tires)) { // skip this, handled } else if (prop.PropertyType == typeof(Chassis)) { // skip this, handled } else { UnhandledDataTypes.Add(new UnhandledDataTypeError() { Field = prop.Name, Value = values[prop.Name.ToString()].ToString(), DataType = prop.PropertyType.Name }); //Console.WriteLine($"Unhandled data type: {prop.PropertyType.Name}"); } } } else { Console.WriteLine($"No value for {propKey}"); } } catch (FormatException) { FormatExceptions.Add(new FormatExceptionError() { Field = prop.Name, Value = values[prop.Name.ToString()].ToString() }); } catch (Exception ex) { string errorValue = "MISSING"; if (values.ContainsKey("")) { errorValue = values[prop.Name.ToString()].ToString(); } Console.WriteLine($"{propKey}: [{errorValue}] {ex.ToString()}"); } } }