public object Deserialize(string serializedObject) { if (serializedObject == null) { throw new ArgumentNullException("serializedObject"); } Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedObject); if ((dic.ContainsKey("SerializerHandlerType") == false) || (dic.ContainsKey("SerializedObject") == false)) { throw new InvalidOperationException("serializedObject is of wrong format"); } string serilizerHandlerTypeString = StringConversionServices.DeserializeValueString(dic["SerializerHandlerType"]); Type serilizerHandlerType = TypeManager.GetType(serilizerHandlerTypeString); ISerializerHandler serializerHandler; lock (_lock) { if (_serializeHandlers.TryGetValue(serilizerHandlerType, out serializerHandler) == false) { serializerHandler = (ISerializerHandler)Activator.CreateInstance(serilizerHandlerType); _serializeHandlers.Add(serilizerHandlerType, serializerHandler); } } string serializedObjectString = StringConversionServices.DeserializeValueString(dic["SerializedObject"]); object resultObject = serializerHandler.Deserialize(serializedObjectString); return(resultObject); }
public object Deserialize(string serializedObject) { Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedObject); if (dic.ContainsKey("_Type_") == false) { throw new ArgumentException("serializedObject is of wrong format"); } string typeString = StringConversionServices.DeserializeValueString(dic["_Type_"]); Type type = TypeManager.GetType(typeString); object obj = Activator.CreateInstance(type); IEnumerable <PropertyInfo> propertyInfos = from prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) where prop.CanRead && prop.CanWrite select prop; foreach (PropertyInfo propertyInfo in propertyInfos) { if (dic.ContainsKey(propertyInfo.Name) == false) { throw new ArgumentException("serializedObject is of wrong format"); } MethodInfo methodInfo = (from mi in typeof(StringConversionServices).GetMethods(BindingFlags.Public | BindingFlags.Static) where mi.Name == "DeserializeValue" && mi.IsGenericMethodDefinition && mi.GetParameters().Length == 2 && mi.GetParameters()[1].ParameterType.IsGenericParameter select mi).SingleOrDefault(); object defaultValue; if (propertyInfo.PropertyType == typeof(Guid)) { defaultValue = default(Guid); } else if (propertyInfo.PropertyType == typeof(string)) { defaultValue = default(string); } else if (propertyInfo.PropertyType == typeof(int)) { defaultValue = default(int); } else if (propertyInfo.PropertyType == typeof(DateTime)) { defaultValue = default(DateTime); } else if (propertyInfo.PropertyType == typeof(bool)) { defaultValue = default(bool); } else if (propertyInfo.PropertyType == typeof(decimal)) { defaultValue = default(decimal); } else if (propertyInfo.PropertyType == typeof(long)) { defaultValue = default(long); } else { defaultValue = null; } methodInfo = methodInfo.MakeGenericMethod(new Type[] { propertyInfo.PropertyType }); object propertyValue = methodInfo.Invoke(null, new object[] { dic[propertyInfo.Name], defaultValue }); propertyInfo.SetValue(obj, propertyValue, null); } return(obj); }