public RpcObjectProxyFactory GenerateObjectProxyFactory <TService>(
            IReadOnlyCollection <string>?implementedServices,
            IReadOnlyDictionary <string, ImmutableArray <Type> >?knownServiceTypes)
            where TService : class
        {
            lock (this.syncRoot)
            {
                var serviceInterfaces = GetAllServices <TService>(implementedServices, knownServiceTypes);
                var key = new HashSetKey <Type>(serviceInterfaces.Select(s => s.Type));

                // If a proxy with the same set of service interfaces has been generated before
                // let's reuse that one.
                if (this.generatedFactories.TryGetValue(key, out var currFactory))
                {
                    // TODO: Should maybe look for a factory which has a superset of implemented interfaces?
                    return((RpcObjectProxyFactory)currFactory);
                }

                var(moduleBuilder, definedProxyTypes) = CreateModuleBuilder();

                var proxyTypeBuilder = new RpcServiceProxyBuilder <TRpcProxy, TMethodDef>(
                    serviceInterfaces,
                    moduleBuilder, definedProxyTypes);
                (Func <TProxyArgs, TMethodDef[], RpcProxyBase> proxyCreator, TMethodDef[] proxyMethodDefs)
                    = proxyTypeBuilder.BuildObjectProxyFactory <TProxyArgs>();

                RpcObjectProxyFactory newFactory = this.CreateProxyFactory(proxyCreator, implementedServices, proxyMethodDefs);

                this.generatedFactories.Add(key, newFactory);

                return(newFactory);
            }
        }
        private (TService, Mock <TestCallInvoker>) CreateServiceInstance <TService>() where TService : class
        {
            var(moduleBuilder, definedTypes) = this.CreateModuleBuilder();
            var proxyBuilder = new RpcServiceProxyBuilder <GrpcProxyBase, GrpcProxyMethod>(RpcBuilderUtil.GetAllServices <TService>(true), moduleBuilder, definedTypes);

            var(proxyType, proxyMethods) = proxyBuilder.BuildObjectProxyType(new Type[] { typeof(GrpcProxyArgs), typeof(GrpcProxyMethod[]) });

            ValidateProxyType <TService>(proxyType);

            var factory = RpcServiceProxyBuilder <GrpcProxyBase, GrpcProxyMethod> .CreateObjectProxyFactory <GrpcProxyArgs>(proxyType);

            var callInvokerMock = new Mock <TestCallInvoker>(MockBehavior.Strict);
            var connectionMock  = new Mock <IRpcConnection>(MockBehavior.Strict);

            connectionMock.Setup(m => m.Options).Returns(ImmutableRpcClientOptions.Empty);
            var serializer = new ProtobufRpcSerializer();

            var args = new GrpcProxyArgs
                       (
                objectId: RpcObjectId.NewId(),
                connection: connectionMock.Object,
                callInvoker: callInvokerMock.Object,
                serializer: serializer,
                methodsCache: new GrpcMethodsCache(serializer),
                implementedServices: null,
                syncContext: null
                       );

            var serviceInstance = factory(args, proxyMethods);

            return((TService)(object)serviceInstance, callInvokerMock);
        }