List <Value> ConvertToValues(IDictionary <string, IItem> items) { List <Value> values = new List <Value>(); bool add = true; foreach (var item in items) { Value value; if (item.Value is Data.BoolValue) { BoolValue v = new BoolValue(); v.Value = ((Data.BoolValue)item.Value).Value; value = v; } else if (item.Value is Data.IntValue) { IntValue v = new IntValue(); v.Value = ((Data.IntValue)item.Value).Value; value = v; } else if (item.Value is Data.TimeSpanValue) { TimeSpanValue v = new TimeSpanValue(); v.Value = (long)((Data.TimeSpanValue)item.Value).Value.TotalSeconds; value = v; } else if (item.Value is Data.PercentValue) { PercentValue v = new PercentValue(); v.Value = ((Data.PercentValue)item.Value).Value; value = v; if (double.IsNaN(v.Value)) { add = false; } } else if (item.Value is Data.DoubleValue) { DoubleValue v = new DoubleValue(); v.Value = ((Data.DoubleValue)item.Value).Value; value = v; if (double.IsNaN(v.Value)) { add = false; } } else if (item.Value is Data.StringValue) { StringValue v = new StringValue(); v.Value = ((Data.StringValue)item.Value).Value; value = v; } else { BinaryValue v = new BinaryValue(); using (MemoryStream stream = new MemoryStream()) { XmlGenerator.Serialize(item.Value, stream); stream.Close(); v.Value = stream.ToArray(); } value = v; } if (add) { value.Name = item.Key; value.DataType = new DataType(); value.DataType.Name = item.Value.GetType().Name; value.DataType.TypeName = item.Value.GetType().AssemblyQualifiedName; values.Add(value); } else { add = true; } } return(values); }
public Value ConvertToValue(IItem item, string name) { Value result = null; if (item is ValueTypeValue <bool> ) { var boolValue = (ValueTypeValue <bool>)item; result = new BoolValue() { Value = boolValue.Value }; } else if (item is ValueTypeValue <int> ) { var intValue = (ValueTypeValue <int>)item; result = new IntValue() { Value = intValue.Value }; } else if (item is ValueTypeValue <long> ) { var longValue = (ValueTypeValue <long>)item; result = new LongValue() { Value = longValue.Value }; } else if (item is ValueTypeValue <float> ) { var floatValue = (ValueTypeValue <float>)item; result = new FloatValue() { Value = floatValue.Value }; } else if (item is ValueTypeValue <double> ) { var doubleValue = (ValueTypeValue <double>)item; if (item is Data.PercentValue) { result = new PercentValue() { Value = doubleValue.Value } } ; else { result = new DoubleValue() { Value = doubleValue.Value } }; } else if (item is ValueTypeValue <TimeSpan> ) { var timeSpanValue = (ValueTypeValue <TimeSpan>)item; result = new TimeSpanValue() { Value = (long)timeSpanValue.Value.TotalSeconds }; } else if (item is Data.StringValue) { var stringValue = (Data.StringValue)item; result = new StringValue() { Value = stringValue.Value }; } if (result == null) { var binaryValue = new BinaryValue { DataType = new DataType() { Name = item.GetType().Name, TypeName = item.GetType().AssemblyQualifiedName } }; using (var memStream = new MemoryStream()) { XmlGenerator.Serialize(item, memStream); binaryValue.Value = memStream.ToArray(); } result = binaryValue; } result.Name = name; return(result); }