object IProxyFactory.GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, object[] constructorArguments)
        {
            // TODO:
            //  * new type MockCtorPlaceholder in elevated assy
            //  * generate new empty ctor that takes MockCtorPlaceholder in all mocked types
            //  * support ctor params. throw if foudn and not ForPartsOf. then ForPartsOf determines which ctor we use.
            //  * have a note about static ctors. because they are special, and do not support disposal, can't really mock them right.
            //    best for user to do mock/unmock of static ctors manually (i.e. move into StaticInit/StaticDispose and call directly from test code)

            object proxy;
            var    substituteConfig = ElevatedSubstitutionContext.TryGetSubstituteConfig(callRouter);

            if (typeToProxy.IsInterface || substituteConfig == null)
            {
                proxy = m_DefaultProxyFactory.GenerateProxy(callRouter, typeToProxy, additionalInterfaces, constructorArguments);
            }
            else if (typeToProxy == typeof(SubstituteStatic.Proxy))
            {
                if (additionalInterfaces != null && additionalInterfaces.Any())
                {
                    throw new SubstituteException("Cannot substitute interfaces as static");
                }
                if (constructorArguments.Length != 1)
                {
                    throw new SubstituteException("Unexpected use of SubstituteStatic.For");
                }

                // the type we want comes from SubstituteStatic.For as a single ctor arg
                var actualType = (Type)constructorArguments[0];

                proxy = CreateStaticProxy(actualType, callRouter);
            }
            else
            {
                // requests for additional interfaces on patched types cannot be done at runtime. elevated mocking can't,
                // by definition, go through a runtime dynamic proxy generator that could add such things.
                if (additionalInterfaces.Any())
                {
                    throw new SubstituteException("Cannot add interfaces at runtime to patched types");
                }

                if (substituteConfig == SubstituteConfig.OverrideAllCalls)
                {
                    // overriding all calls includes the ctor, so it makes no sense for the user to pass in ctor args
                    if (constructorArguments.Any())
                    {
                        throw new SubstituteException("Do not pass ctor args when substituting with elevated mocks (or did you mean to use ForPartsOf?)");
                    }

                    // but we use a ctor arg to select the special empty ctor that we patched in
                    constructorArguments = k_MockedCtorParams;
                }

                proxy = Activator.CreateInstance(typeToProxy, constructorArguments);
                GetRouterField(typeToProxy).SetValue(proxy, callRouter);
            }

            return(proxy);
        }
Пример #2
0
        public object GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[]?additionalInterfaces, object?[]?constructorArguments)
        {
            var isDelegate = typeToProxy.IsDelegate();

            return(isDelegate
                ? _delegateFactory.GenerateProxy(callRouter, typeToProxy, additionalInterfaces, constructorArguments)
                : _dynamicProxyFactory.GenerateProxy(callRouter, typeToProxy, additionalInterfaces, constructorArguments));
        }
Пример #3
0
        public object GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, object[] constructorArguments)
        {
            var isDelegate = typeToProxy.GetTypeInfo().IsSubclassOf(typeof(Delegate));

            return(isDelegate
                ? _delegateFactory.GenerateProxy(callRouter, typeToProxy, additionalInterfaces, constructorArguments)
                : _dynamicProxyFactory.GenerateProxy(callRouter, typeToProxy, additionalInterfaces, constructorArguments));
        }
Пример #4
0
        private object Create(Type[] typesToProxy, object[] constructorArguments, SubstituteConfig config)
        {
            var callRouter       = _callRouterFactory.Create(_context, config);
            var primaryProxyType = GetPrimaryProxyType(typesToProxy);
            var additionalTypes  = typesToProxy.Where(x => x != primaryProxyType).ToArray();
            var proxy            = _proxyFactory.GenerateProxy(callRouter, primaryProxyType, additionalTypes, constructorArguments);

            return(proxy);
        }
Пример #5
0
        private object DelegateProxy(Type delegateType, ICallRouter callRouter)
        {
            var delegateContainer = _delegateContainerCache.GetOrAdd(delegateType, GenerateDelegateContainerInterface);
            var invokeMethod      = delegateContainer.GetMethod(MethodNameInsideProxyContainer);

            var proxy = _objectProxyFactory.GenerateProxy(callRouter, delegateContainer, Type.EmptyTypes, null);

            return(invokeMethod.CreateDelegate(delegateType, proxy));
        }
        public object Create(Type[] typesToProxy, object[] constructorArguments)
        {
            var callRouter       = _callRouterFactory.Create(_context);
            var primaryProxyType = GetPrimaryProxyType(typesToProxy);
            var additionalTypes  = typesToProxy.Where(x => x != primaryProxyType).ToArray();
            var proxy            = _proxyFactory.GenerateProxy(callRouter, primaryProxyType, additionalTypes, constructorArguments);

            _callRouterResolver.Register(proxy, callRouter);
            return(proxy);
        }
Пример #7
0
        private object Create(Type[] typesToProxy, object[] constructorArguments, bool callBaseByDefault)
        {
            var substituteState = _substituteStateFactory.Create(this);

            substituteState.CallBaseConfiguration.CallBaseByDefault = callBaseByDefault;

            var callRouter       = _callRouterFactory.Create(substituteState);
            var primaryProxyType = GetPrimaryProxyType(typesToProxy);
            var additionalTypes  = typesToProxy.Where(x => x != primaryProxyType).ToArray();
            var proxy            = _proxyFactory.GenerateProxy(callRouter, primaryProxyType, additionalTypes, constructorArguments);

            return(proxy);
        }
Пример #8
0
        public object GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces,
                                    object[] constructorArguments)
        {
            var diagCallRouter = new DiagnosticsCallRouter(callRouter, _ctx);
            var result         = _impl.GenerateProxy(diagCallRouter, typeToProxy, additionalInterfaces, constructorArguments);

            _ctx.RegisterPrimaryProxyType(result, typeToProxy);

            LogAndTrace(
                $"GenerateProxy(callRouter: {callRouter.DiagName(_ctx)}, " +
                $"typeToProxy: {typeToProxy.FullName}, " +
                $"additionalInterfaces: {additionalInterfaces.Print(x => x.FullName)}, " +
                $"constructorArguments: {constructorArguments.Print(a => a.GetObjectId(_ctx))}) " +
                $"=> {result.SubstituteId(_ctx)}");

            _ctx.MapRouterToSubstitute(callRouter, result);
            _ctx.MapRouterToSubstitute(diagCallRouter, result);
            _ctx.MapRouterToDiagRouter(callRouter, diagCallRouter);

            return(result);
        }