/// <summary> /// Deserializes a value with the provided type from the specified element. /// </summary> /// <param name="type">The data type.</param> /// <param name="element">The element.</param> /// <returns></returns> internal static object DeserializeValue(Type type, XElement element) { string argVal = element.Value; if (type == typeof(string)) { return(argVal); } else if (type == typeof(sbyte)) { return(sbyte.Parse(argVal)); } else if (type == typeof(short)) { return(short.Parse(argVal)); } else if (type == typeof(int)) { return(int.Parse(argVal)); } else if (type == typeof(long)) { return(long.Parse(argVal)); } else if (type == typeof(byte)) { return(byte.Parse(argVal)); } else if (type == typeof(ushort)) { return(ushort.Parse(argVal)); } else if (type == typeof(uint)) { return(uint.Parse(argVal)); } else if (type == typeof(ulong)) { return(ulong.Parse(argVal)); } else if (type == typeof(bool)) { return(bool.Parse(argVal)); } else if (type == typeof(void)) { return(null); } else if (type == typeof(Guid)) { return(Guid.Parse(argVal)); } else if (type == typeof(ServiceAddress)) { return(ServiceAddress.Parse(argVal)); } else { // read xml from argument XmlReader contentReader = element.CreateReader(); contentReader.MoveToContent(); string xml = contentReader.ReadInnerXml(); // deserialize XmlSerializer valueDeserializer = new XmlSerializer(type); using (StringReader valueStream = new StringReader(xml)) { return(valueDeserializer.Deserialize(valueStream)); } } }
/// <summary> /// Gets the data as a .NET type. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> public object GetData(Type type) { if (Data == null) { return(null); } else if (type == typeof(string)) { return(Encoding.UTF8.GetString(Data)); } else if (type == typeof(sbyte)) { return((sbyte)Data[0]); } else if (type == typeof(short)) { return(BitConverter.ToInt16(Data, 0)); } else if (type == typeof(int)) { return(BitConverter.ToInt32(Data, 0)); } else if (type == typeof(long)) { return(BitConverter.ToInt64(Data, 0)); } else if (type == typeof(byte)) { return(Data[0]); } else if (type == typeof(ushort)) { return(BitConverter.ToUInt16(Data, 0)); } else if (type == typeof(uint)) { return(BitConverter.ToUInt32(Data, 0)); } else if (type == typeof(ulong)) { return(BitConverter.ToUInt64(Data, 0)); } else if (type == typeof(double)) { return(BitConverter.ToDouble(Data, 0)); } else if (type == typeof(bool)) { return(Data[0] == 1); } else if (type == typeof(void)) { return(null); } else if (type == typeof(Guid)) { return(new Guid(Data)); } else if (type == typeof(ServiceAddress)) { return(ServiceAddress.Parse(Encoding.UTF8.GetString(Data))); } else if (type == typeof(byte[])) { return(Data); } else if (type == typeof(DateTime)) { return(DateTime.Parse(Encoding.UTF8.GetString(Data))); } else { TypeInfo typeInfo = type.GetTypeInfo(); if (typeInfo.GetCustomAttribute <ProtoContractAttribute>() != null) { using (MemoryStream ms = new MemoryStream(Data)) { return(Serializer.Deserialize(type, ms)); } } else { return(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(Data), type)); } } }