private string Serializer(Type type, object value) { if (value == null) return null; string retVal = null; //Try to simplify serialization for simple types first without using xmlserializer. if (typeof(string) == type) retVal = value as string; else if (typeof(Type) == type) retVal = ((Type)value).AssemblyQualifiedName; else if (type.IsEnum) retVal = value.ToString(); else retVal = TrySerializePrimitive(type, value); if (retVal == null) { using (var writer = new StringWriter(CultureInfo.InvariantCulture)) { try { XmlSerializer xmlserializer = new XmlSerializer(type); xmlserializer.Serialize(writer, value); retVal = writer.ToString(); } catch (Exception ex) { var configEx = new ConfigurationException("Error on serializing configuration data", ex); throw configEx; } } } return retVal; }
private object Deserializer(Type type, string value) { object retVal = null; // First try to deserialize using simple types first, then use xml serializer if (value == null) return null; else if (typeof(string) == type) retVal = value; else if (typeof(Type) == type) retVal = Type.GetType(value); else if (type.IsEnum) retVal = Enum.Parse(type, value); if (retVal == null) retVal = TryParsePrimitive(type, value); if (retVal == null) { using (var reader = new StringReader(value)) { try { XmlSerializer xmlserializer = new XmlSerializer(type); retVal = xmlserializer.Deserialize(reader); } catch (Exception ex) { var configEx = new ConfigurationException("Error on deserializing configuration data", ex); throw configEx; } } } return retVal; }