public static void Deserialize(ListTreeNode <T> src, ref U t) { if (s_d == null) { if (typeof(U).IsEnum) { // enum from string var mi = typeof(Enum).GetMethods(BindingFlags.Static | BindingFlags.Public).First( x => x.Name == "Parse" && x.GetParameters().Length == 3 ); var enumParse = GenericInvokeCallFactory.StaticFunc <Type, string, bool, object>(mi); s_d = x => { var enumValue = enumParse(typeof(U), x.GetString(), true); return(GenericCast <object, U> .Cast(enumValue)); }; } else { s_d = x => GenericCast <string, U> .Cast(x.GetString()); } } t = s_d(src); }
private static FieldSerializer CreateFieldSerializer(FieldInfo fi) { var mi = typeof(Serializer).GetMethod("_CreateFieldSerializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(fi.FieldType); return(GenericInvokeCallFactory.StaticFunc <FieldInfo, FieldSerializer>(g)(fi)); }
private static Func <ListTreeNode <T>, U> GetCreator() { var t = typeof(U); if (t.IsArray) { var mi = typeof(GenericConstructor <T, U>).GetMethod("ArrayCreator", BindingFlags.NonPublic | BindingFlags.Static); var g = mi.MakeGenericMethod(t.GetElementType()); return(GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, U>(g)); } { return(_s => { return Activator.CreateInstance <U>(); }); } }
private static Func <ListTreeNode <T>, U> GetDeserializer() { // primitive { var mi = typeof(ListTreeNode <T>).GetMethods().FirstOrDefault(x => { if (!x.Name.StartsWith("Get")) { return(false); } if (!x.Name.EndsWith(typeof(U).Name)) { return(false); } var parameters = x.GetParameters(); if (parameters.Length != 0) { return(false); } if (x.ReturnType != typeof(U)) { return(false); } return(true); }); if (mi != null) { return(GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, U>(mi)); } } var target = typeof(U); if (target.IsArray) { var mi = typeof(GenericDeserializer <T, U>).GetMethod("GenericArrayDeserializer", BindingFlags.Static | BindingFlags.Public); var g = mi.MakeGenericMethod(target.GetElementType()); return(GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, U>(g)); } if (target.IsGenericType) { if (target.GetGenericTypeDefinition() == typeof(List <>)) { var mi = typeof(GenericDeserializer <T, U>).GetMethod("GenericListDeserializer", BindingFlags.Static | BindingFlags.Public); var g = mi.MakeGenericMethod(target.GetGenericArguments()); return(GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, U>(g)); } if (target == typeof(Dictionary <string, object>)) { var mi = typeof(GenericDeserializer <T, U>).GetMethod("DefaultDictionaryDeserializer", BindingFlags.Static | BindingFlags.Public); var func = GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, object>(mi); // object to Dictionary<string, object> return(x => (U)func(x)); } else if (target.GetGenericTypeDefinition() == typeof(Dictionary <,>) && target.GetGenericArguments()[0] == typeof(string)) { var mi = typeof(GenericDeserializer <T, U>).GetMethod("DictionaryDeserializer", BindingFlags.Static | BindingFlags.Public); var g = mi.MakeGenericMethod(target.GetGenericArguments()[1]); return(GenericInvokeCallFactory.StaticFunc <ListTreeNode <T>, U>(g)); } } { var schema = JsonSchema.FromType <U>(); return(s => { var t = default(U); schema.Validator.Deserialize(s, ref t); return t; }); } #if false if (target.IsEnum) { var value = Expression.Parameter(typeof(int), "value"); var cast = Expression.Convert(value, target); var func = Expression.Lambda(cast, value); var compiled = (Func <int, T>)func.Compile(); return(s => { return compiled(s.GetInt32()); }); } { var fields = target.GetFields(BindingFlags.Instance | BindingFlags.Public); var fieldDeserializers = fields.ToDictionary(x => Utf8String.From(x.Name), x => { var mi = typeof(GenericDeserializer <S, T>).GetMethod("GetFieldDeserializer", BindingFlags.Static | BindingFlags.NonPublic); var g = mi.MakeGenericMethod(x.FieldType); return((FieldSetter)g.Invoke(null, new object[] { x })); }); return((S s) => { if (!s.IsMap()) { throw new ArgumentException(s.ValueType.ToString()); } var t = (object)default(GenericCreator <S, T>).Create(s); foreach (var kv in s.ObjectItems()) { FieldSetter setter; if (fieldDeserializers.TryGetValue(kv.Key, out setter)) { setter(kv.Value, t); } } return (T)t; }); } #endif }