Exemplo n.º 1
0
            public IJsonFormatter <T> GetFormatter <T>()
#endif
            {
                if (formattersCache.TryGetValue(typeof(T), out var formatter))
                {
                    goto RETURN;
                }

                foreach (var subFormatter in subFormatters)
                {
                    if (!(subFormatter is IJsonFormatter <T>))
                    {
                        continue;
                    }

                    formatter = subFormatter;
                    goto CACHE;
                }

                foreach (var resolver in subResolvers)
                {
                    formatter = resolver.GetFormatter <T>();
                    if (formatter != null)
                    {
                        goto CACHE;
                    }
                }

                // when not found, cache null.
CACHE:
                formattersCache.TryAdd(new ThreadSafeTypeKeyReferenceHashTable <IJsonFormatter> .Entry(typeof(T), formatter));

RETURN:
                return(formatter as IJsonFormatter <T>);
            }
Exemplo n.º 2
0
        public static object GetFormatterDynamic(this IFormatterResolver resolver, Type type)
#endif
        {
            if (formatterGetters.TryGetValue(type, out var formatterGetter))
            {
#if CSHARP_8_OR_NEWER
                return(formatterGetter !(resolver));
#else
                return(formatterGetter(resolver));
#endif
            }

            var getFormatterGenericInstanceMethod = getFormatterRuntimeMethod.MakeGeneric(type);
            var inputResolver = Expression.Parameter(typeof(IFormatterResolver), "inputResolver");
#if CSHARP_8_OR_NEWER
            formatterGetter = Expression.Lambda <Func <IFormatterResolver, IJsonFormatter?> >(Expression.Call(inputResolver, getFormatterGenericInstanceMethod), inputResolver).Compile();
            formatterGetters.TryAdd(new ThreadSafeTypeKeyReferenceHashTable <Func <IFormatterResolver, IJsonFormatter?> > .Entry(type, formatterGetter));
#else
            formatterGetter = Expression.Lambda <Func <IFormatterResolver, IJsonFormatter> >(Expression.Call(inputResolver, getFormatterGenericInstanceMethod), inputResolver).Compile();
            formatterGetters.TryAdd(new ThreadSafeTypeKeyReferenceHashTable <Func <IFormatterResolver, IJsonFormatter> > .Entry(type, formatterGetter));
#endif

            return(formatterGetter(resolver));
        }