private static string GetDescription(object instance) { var dto = instance as DTO; return(dto?.ToString() ?? DTO.ToString(instance)); }
/// <summary> /// Converts a value to its XML string representation. /// </summary> /// <param name="Value">Value</param> /// <returns>XML string representation.</returns> public string ValueToXmlString(object Value) { switch (this.dataType) { case "ui1": case "ui2": case "ui4": case "i1": case "i2": case "i4": case "int": case "char": case "string": case "uri": case "uuid": default: return(Value.ToString()); case "r4": case "r8": case "number": case "float": if (!(Value is double d)) { if (Value is float f) { d = f; } else if (Value is decimal d2) { d = (double)d2; } else { d = Convert.ToDouble(Value); } } return(d.ToString().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".")); case "fixed.14.4": if (Value is double d3) { d = d3; } else if (Value is float f2) { d = f2; } else if (Value is decimal d4) { d = (double)d4; } else { d = Convert.ToDouble(Value); } return(d.ToString("F4").Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".")); case "date": if (!(Value is DateTime DT)) { DT = Convert.ToDateTime(Value); } return(DT.ToString("yyyyMMdd")); case "dateTime": if (!(Value is DateTime DT2)) { DT2 = Convert.ToDateTime(Value); } return(DT2.ToString("yyyyMMddTHHmmss")); case "dateTime.tz": if (Value is DateTimeOffset DTO) { string s = DTO.ToString("yyyyMMddTHHmmss"); TimeSpan Zone = DTO.Offset; if (Zone < TimeSpan.Zero) { s += "-"; Zone = -Zone; } else { s += "+"; } return(s + Zone.Hours.ToString("D2") + ":" + Zone.Minutes.ToString("D2")); } else { if (!(Value is DateTime DT3)) { DT3 = Convert.ToDateTime(Value); } return(DT3.ToString("yyyyMMddTHHmmss")); } case "time": if (Value is TimeSpan TS) { return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else if (Value is DateTime DT4) { return(DT4.ToString("HH:mm:ss")); } else if (TimeSpan.TryParse(Value.ToString(), out TS)) { return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else { DT = Convert.ToDateTime(Value); return(DT.ToString("HH:mm:ss")); } case "time.tz": if (Value is TimeSpan TS2) { return(TS2.Hours.ToString("D2") + ":" + TS2.Minutes.ToString("D2") + ":" + TS2.Seconds.ToString("D2")); } else if (Value is DateTime DT5) { return(DT5.ToString("HH:mm:ss")); } else if (Value is DateTimeOffset DTO2) { string s = DTO2.ToString("HH:mm:ss"); TimeSpan Zone = DTO2.Offset; if (Zone < TimeSpan.Zero) { s += "-"; Zone = -Zone; } else { s += "+"; } return(s + Zone.Hours.ToString("D2") + ":" + Zone.Minutes.ToString("D2")); } else if (TimeSpan.TryParse(Value.ToString(), out TS)) { return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else { DT = Convert.ToDateTime(Value); return(DT.ToString("HH:mm:ss")); } case "boolean": if (!(Value is bool b)) { b = Convert.ToBoolean(Value); } return(b ? "1" : "0"); case "bin.base64": if (!(Value is byte[] Bin)) { Bin = SerializeToBinary(Value); } return(Convert.ToBase64String(Bin, Base64FormattingOptions.None)); case "bin.hex": Bin = Value as byte[]; if (Bin is null) { Bin = SerializeToBinary(Value); } StringBuilder sb = new StringBuilder(); foreach (byte b2 in Bin) { sb.Append(b2.ToString("X2")); } return(sb.ToString()); } }
/// <summary> /// Converts a value to its XML string representation. /// </summary> /// <param name="Value">Value</param> /// <returns>XML string representation.</returns> public string ValueToXmlString(object Value) { switch (this.dataType) { case "ui1": case "ui2": case "ui4": case "i1": case "i2": case "i4": case "int": case "char": case "string": case "uri": case "uuid": default: return(Value.ToString()); case "r4": case "r8": case "number": case "float": double d; if (Value is double) { d = (double)Value; } else if (Value is float) { d = (float)Value; } else if (Value is decimal) { d = (double)((decimal)Value); } else { d = System.Convert.ToDouble(Value); } return(d.ToString().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".")); case "fixed.14.4": if (Value is double) { d = (double)Value; } else if (Value is float) { d = (float)Value; } else if (Value is decimal) { d = (double)((decimal)Value); } else { d = System.Convert.ToDouble(Value); } return(d.ToString("F4").Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".")); case "date": DateTime DT; if (Value is DateTime) { DT = (DateTime)Value; } else { DT = System.Convert.ToDateTime(Value); } return(DT.ToString("yyyyMMdd")); case "dateTime": if (Value is DateTime) { DT = (DateTime)Value; } else { DT = System.Convert.ToDateTime(Value); } return(DT.ToString("yyyyMMddTHHmmss")); case "dateTime.tz": DateTimeOffset DTO; if (Value is DateTimeOffset) { DTO = (DateTimeOffset)Value; string s = DTO.ToString("yyyyMMddTHHmmss"); TimeSpan Zone = DTO.Offset; if (Zone < TimeSpan.Zero) { s += "-"; Zone = -Zone; } else { s += "+"; } return(s + Zone.Hours.ToString("D2") + ":" + Zone.Minutes.ToString("D2")); } else { if (Value is DateTime) { DT = (DateTime)Value; } else { DT = System.Convert.ToDateTime(Value); } return(DT.ToString("yyyyMMddTHHmmss")); } case "time": TimeSpan TS; if (Value is TimeSpan) { TS = (TimeSpan)Value; return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else if (Value is DateTime) { DT = (DateTime)Value; return(DT.ToString("HH:mm:ss")); } else if (TimeSpan.TryParse(Value.ToString(), out TS)) { return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else { DT = System.Convert.ToDateTime(Value); return(DT.ToString("HH:mm:ss")); } case "time.tz": if (Value is TimeSpan) { TS = (TimeSpan)Value; return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else if (Value is DateTime) { DT = (DateTime)Value; return(DT.ToString("HH:mm:ss")); } else if (Value is DateTimeOffset) { DTO = (DateTimeOffset)Value; string s = DTO.ToString("HH:mm:ss"); TimeSpan Zone = DTO.Offset; if (Zone < TimeSpan.Zero) { s += "-"; Zone = -Zone; } else { s += "+"; } return(s + Zone.Hours.ToString("D2") + ":" + Zone.Minutes.ToString("D2")); } else if (TimeSpan.TryParse(Value.ToString(), out TS)) { return(TS.Hours.ToString("D2") + ":" + TS.Minutes.ToString("D2") + ":" + TS.Seconds.ToString("D2")); } else { DT = System.Convert.ToDateTime(Value); return(DT.ToString("HH:mm:ss")); } case "boolean": bool b; if (Value is bool) { b = (bool)Value; } else { b = System.Convert.ToBoolean(Value); } return(b ? "1" : "0"); case "bin.base64": byte[] Bin; Bin = Value as byte[]; if (Bin is null) { Bin = SerializeToBinary(Value); } return(System.Convert.ToBase64String(Bin)); case "bin.hex": Bin = Value as byte[]; if (Bin is null) { Bin = SerializeToBinary(Value); } StringBuilder sb = new StringBuilder(); foreach (byte b2 in Bin) { sb.Append(b2.ToString("X2")); } return(sb.ToString()); } }
public void Recieve_ExampleDTO(DTO.ExampleDTO exampleDto) { logger.Debug("Recieved ExampleDTO => " + exampleDto.ToString()); }