Exemple #1
0
        static Formatter()
        {
            var    t         = typeof(T);
            object formatter = null;
            var    resolver  = ResolverCache <TTypeResolver> .Default;

            try
            {
                formatter = resolver.ResolveFormatter(t);

                // If Unity, should avoid long static constructor and <T> code because IL2CPP generate every <T>.
                if (formatter == null && resolver.IsUseBuiltinSerializer)
                {
                    formatter = Formatter.GetBuiltinFormatter <TTypeResolver
#if !UNITY
                                                               , T
#endif
                                                               >(t, resolver);
                }

                if (formatter == null)
                {
                    formatter = new ErrorFormatter <TTypeResolver, T>();
                }
            }
            catch (Exception ex)
            {
                formatter = new ErrorFormatter <TTypeResolver, T>(ex);
            }

            Default = (Formatter <TTypeResolver, T>)formatter;
        }
Exemple #2
0
        static Formatter()
        {
            var    t         = typeof(T);
            object formatter = null;
            var    resolver  = ResolverCache <TTypeResolver> .Default;

            try
            {
#if NET_CORE
                var ti = t.GetTypeInfo();
#else
                var ti = t;
#endif
                var attribute = (ZeroFormattableAttribute)ti.GetCustomAttributes(typeof(ZeroFormattableAttribute))
                                .FirstOrDefault();
                if (attribute != null && attribute.FormatterType != null)
                {
                    Type[] genericArguments;
                    if (ti.IsGenericType)
                    {
                        var classTypes = ti.GetGenericArguments();
                        genericArguments    = new Type[1 + classTypes.Length];
                        genericArguments[0] = typeof(TTypeResolver);
                        Array.Copy(classTypes, 0, genericArguments, 1, classTypes.Length);
                    }
                    else
                    {
                        genericArguments = new[] { typeof(TTypeResolver) }
                    };

                    formatter = Activator.CreateInstance(
                        attribute.FormatterType.MakeGenericType(genericArguments));
                }

                if (formatter == null)
                {
                    formatter = resolver.ResolveFormatter(t);
                }

                // If Unity, should avoid long static constructor and <T> code because IL2CPP generate every <T>.
                if (formatter == null && resolver.IsUseBuiltinSerializer)
                {
                    formatter = Formatter.GetBuiltinFormatter <TTypeResolver
#if !UNITY
                                                               , T
#endif
                                                               >(t, resolver);
                }

                if (formatter == null)
                {
                    formatter = new ErrorFormatter <TTypeResolver, T>();
                }
            }
            catch (Exception ex)
            {
                formatter = new ErrorFormatter <TTypeResolver, T>(ex);
            }

            Default = (Formatter <TTypeResolver, T>)formatter;
        }