Пример #1
0
        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();
        }
        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));
                }
            }

            throw new NotImplementedException();
        }