public IMessagePackFormatter? GetFormatter()

in src/StreamJsonRpc/MessagePackFormatter.cs [1401:1447]


            public IMessagePackFormatter<T>? GetFormatter<T>()
            {
                if (typeof(T).IsValueType)
                {
                    return null;
                }

                lock (this.formatters)
                {
                    if (this.formatters.TryGetValue(typeof(T), out object? cachedFormatter))
                    {
                        return (IMessagePackFormatter<T>)cachedFormatter;
                    }
                }

                (Type Type, JsonRpcProxyOptions ProxyOptions, JsonRpcTargetOptions TargetOptions)? matchingCandidate = null;
                foreach ((Type Type, JsonRpcProxyOptions ProxyOptions, JsonRpcTargetOptions TargetOptions) candidate in this.implicitlyMarshaledTypes)
                {
                    if (candidate.Type == typeof(T) ||
                        (candidate.Type.IsGenericTypeDefinition && typeof(T).IsConstructedGenericType && candidate.Type == typeof(T).GetGenericTypeDefinition()))
                    {
                        matchingCandidate = candidate;
                        break;
                    }
                }

                if (!matchingCandidate.HasValue)
                {
                    return null;
                }

                object formatter = Activator.CreateInstance(
                    typeof(RpcMarshalableImplicitFormatter<>).MakeGenericType(typeof(T)),
                    this.formatter,
                    matchingCandidate.Value.ProxyOptions,
                    matchingCandidate.Value.TargetOptions)!;

                lock (this.formatters)
                {
                    if (!this.formatters.TryGetValue(typeof(T), out object? cachedFormatter))
                    {
                        this.formatters.Add(typeof(T), cachedFormatter = formatter);
                    }

                    return (IMessagePackFormatter<T>)cachedFormatter;
                }
            }