/// <summary> /// Serailize an object to a strings. /// </summary> /// <param name="objectToSerialize">The object to serialize.</param> /// <param name="hierarchy">An <see cref="IConfigurationUIHierarchy"/> object.</param> /// <returns>The serailized object to string.</returns> public static string SerializeToString(object objectToSerialize, IConfigurationUIHierarchy hierarchy) { if (objectToSerialize == null) { return(null); } if (objectToSerialize is IEnvironmentalOverridesSerializable) { IEnvironmentalOverridesSerializable serializableInstance = objectToSerialize as IEnvironmentalOverridesSerializable; return(serializableInstance.SerializeToString()); } else if (objectToSerialize is ConfigurationNode) { ConfigurationNode node = objectToSerialize as ConfigurationNode; return(CreatePathRelativeToRootNode(node.Path, hierarchy)); } else { Type targetType = objectToSerialize.GetType(); TypeConverter converter = TypeDescriptor.GetConverter(targetType); if (converter != null) { return(converter.ConvertToInvariantString(objectToSerialize)); } } return(null); }
/// <summary> /// Deserialize content from a string. /// </summary> /// <param name="serializedContents">The serialized content.</param> /// <param name="targetType">The target type.</param> /// <param name="hierarchy">An <see cref="IConfigurationUIHierarchy"/> object.</param> /// <returns>The deserialized content.</returns> public static object DeserializeFromString(string serializedContents, Type targetType, IConfigurationUIHierarchy hierarchy) { if (serializedContents == null) { return(null); } else if (typeof(IEnvironmentalOverridesSerializable).IsAssignableFrom(targetType)) { IEnvironmentalOverridesSerializable instance = (IEnvironmentalOverridesSerializable)Activator.CreateInstance(targetType); instance.DesializeFromString(serializedContents); return(instance); } else if (typeof(ConfigurationNode).IsAssignableFrom(targetType)) { string fullNodePath = SerializationUtility.CreateAbsolutePath(serializedContents, hierarchy); ConfigurationNode foundNode = hierarchy.FindNodeByPath(fullNodePath); if (foundNode != null) { if (targetType.IsAssignableFrom(foundNode.GetType())) { return(foundNode); } } return(null); } else { TypeConverter converter = TypeDescriptor.GetConverter(targetType); if (converter != null) { return(converter.ConvertFromInvariantString(serializedContents)); } return(null); } }