示例#1
0
        public void GenericCallUtilityTestsSimplePasses()
        {
            var s = new Sample();


            {
                var mi     = s.GetType().GetMethod("Set");
                var action = GenericInvokeCallFactory.OpenAction <Sample, int>(mi);
                action(s, 1);
                Assert.AreEqual(1, s.Value);
            }

            {
                var mi   = s.GetType().GetMethod("Get");
                var func = GenericInvokeCallFactory.OpenFunc <Sample, int, int>(mi);
                /*var value =*/
                func(s, 1);
                Assert.AreEqual(1, s.Value);
            }

            {
                var mi     = s.GetType().GetMethod("Set");
                var action = GenericExpressionCallFactory.Create <Sample, int>(mi);
                action(s, 2);
                Assert.AreEqual(2, s.Value);
            }
        }
示例#2
0
            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));
                }
示例#4
0
        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>(); });
            }
        }
示例#5
0
            public static void Serialize(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, T o)
            {
                if (s_serializer == null)
                {
                    var        t = typeof(T);
                    MethodInfo g = null;
                    if (t.IsArray)
                    {
                        var mi = typeof(JsonArrayValidator).GetMethod("ArraySerializer",
                                                                      BindingFlags.Static | BindingFlags.NonPublic);
                        g = mi.MakeGenericMethod(t.GetElementType());
                    }
                    else if (t.GetIsGenericList())
                    {
                        // ToDo: IList
                        var mi = typeof(JsonArrayValidator).GetMethod("ListSerializer",
                                                                      BindingFlags.Static | BindingFlags.NonPublic);
                        g = mi.MakeGenericMethod(t.GetGenericArguments());
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    var compiled = (Action <
                                        IJsonSchemaValidator,
                                        IFormatter,
                                        JsonSchemaValidationContext,
                                        T>)
                                   GenericInvokeCallFactory.StaticAction <
                        IJsonSchemaValidator,
                        IFormatter,
                        JsonSchemaValidationContext,
                        T>(g);
                    s_serializer = new Serializer(compiled);
                }

                s_serializer(v, f, c, o);
            }
示例#6
0
        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
        }
        private static Action <IFormatter, T> GetSerializer()
        {
            var t = typeof(T);

            // object
            if (typeof(T) == typeof(object) && t.GetType() != typeof(object))
            {
                var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject");
                return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi));
            }

            try
            {
                // primitive
                var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t });
                if (mi != null)
                {
                    return(GenericInvokeCallFactory.OpenAction <IFormatter, T>(mi));
                }
            }
            catch (AmbiguousMatchException)
            {
                // do nothing
            }

            {
                // dictionary
                var idictionary = t.GetInterfaces().FirstOrDefault(x =>
                                                                   x.IsGenericType &&
                                                                   x.GetGenericTypeDefinition() == typeof(IDictionary <,>) &&
                                                                   x.GetGenericArguments()[0] == typeof(string)
                                                                   );
                if (idictionary != null)
                {
                    var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary");
                    return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi));
                }
            }

            {
                // object[]
                if (t == typeof(object[]))
                {
                    var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray");
                    return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi));
                }
            }

            {
                // list
                var ienumerable = t.GetInterfaces().FirstOrDefault(x =>
                                                                   x.IsGenericType &&
                                                                   x.GetGenericTypeDefinition() == typeof(IEnumerable <>)
                                                                   );
                if (ienumerable != null)
                {
                    var g  = FormatterExtensionsSerializer.GetMethod("SerializeArray");
                    var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments());
                    return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi));
                }
            }

            {
                // reflection
                var schema = JsonSchema.FromType <T>();
                return((IFormatter f, T value) =>
                {
                    var c = new JsonSchemaValidationContext(value)
                    {
                        EnableDiagnosisForNotRequiredFields = true
                    };
                    schema.Serialize(f, value, c);
                });
            }


            //throw new NotImplementedException();
        }