public object unmarshallByOutType(object input, Type outType) { if (input == null) { return(null); } if (readerByOutType.ContainsKey(outType)) { IEzyReader reader = readerByOutType[outType]; return(reader.read(input, this)); } if (outType.IsGenericType) { if (typeof(IDictionary).IsAssignableFrom(outType) || typeof(IDictionary <,>) == outType.GetGenericTypeDefinition()) { Type dictType = typeof(Dictionary <,>); Type constructed = dictType.MakeGenericType(outType.GetGenericArguments()); IDictionary answer = (IDictionary)Activator.CreateInstance(constructed); EzyObject obj = (EzyObject)input; Type keyType = outType.GetGenericArguments()[0]; Type valueType = outType.GetGenericArguments()[1]; foreach (object key in obj.keys()) { answer[unmarshallByOutType(key, keyType)] = unmarshallByOutType(obj.getByOutType(key, valueType), valueType); } return(answer); } else if (typeof(IList).IsAssignableFrom(outType) || typeof(IList <>) == outType.GetGenericTypeDefinition()) { Type listType = typeof(List <>); Type constructed = listType.MakeGenericType(outType.GetGenericArguments()); IList answer = (IList)Activator.CreateInstance(constructed); EzyArray array = (EzyArray)input; Type valueType = outType.GetGenericArguments()[0]; for (int i = 0; i < array.size(); ++i) { Object rawValue = array.getByOutType(i, valueType); Object value = unmarshallByOutType(rawValue, valueType); answer.Add(value); } return(answer); } } return(input); }
protected override T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller) { PropertyInfo[] properties = objectType.GetProperties(); T obj = (T)Activator.CreateInstance(objectType); foreach (PropertyInfo property in properties) { Type outType = property.PropertyType; object rawValue = null; EzyValue anno = property.GetCustomAttribute <EzyValue>(); if (anno != null) { rawValue = map.getByOutType(anno.name, outType); } else { rawValue = map.getByOutType(property.Name, outType); if (rawValue == null) { string keyString = char.ToLower(property.Name[0]).ToString(); if (property.Name.Length > 1) { keyString += property.Name.Substring(1); } rawValue = map.getByOutType(keyString, outType); } } if (rawValue == null) { continue; } object value = unmarshaller.unmarshallByOutType(rawValue, outType); if (outType == value.GetType()) { property.SetValue(obj, value); } else { MethodInfo parseMethod = property.PropertyType.GetMethod( "TryParse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), property.PropertyType.MakeByRefType() }, null ); if (parseMethod != null) { object[] parameters = new[] { value, null }; bool success = (bool)parseMethod.Invoke(null, parameters); if (success) { property.SetValue(obj, parameters[1]); } } } } return(obj); }