private ProxyGenerationOptions CreateProxyGenerationOptions(Type type, MockCreationSettings settings, MockMixin mockMixinImpl = null) { var options = new ProxyGenerationOptions(); if (mockMixinImpl != null) options.AddMixinInstance(mockMixinImpl); foreach (var mixin in settings.Mixins) options.AddMixinInstance(mixin); if (settings.AdditionalProxyTypeAttributes != null) { foreach (var attr in settings.AdditionalProxyTypeAttributes) { options.AdditionalAttributes.Add(attr); } } return options; }
public object Create(Type type, MocksRepository repository, IMockMixin mockMixinImpl, MockCreationSettings settings, bool createTransparentProxy) { var options = new ProxyGenerationOptions(); options.AddMixinInstance(mockMixinImpl); foreach (var mixin in settings.Mixins) options.AddMixinInstance(mixin); if (settings.AdditionalProxyTypeAttributes != null) { foreach (var attr in settings.AdditionalProxyTypeAttributes) { options.AdditionalAttributes.Add(attr); } } var interceptor = repository.Interceptor; #if SILVERLIGHT options.Hook = new ProxyGenerationHook(false, settings.InterceptorFilter); #else options.Hook = new ProxyGenerationHook(settings.MockConstructorCall, settings.InterceptorFilter); #endif object instance = null; Exception proxyFailure = null; if (type.IsInterface) { if (settings.Args != null && settings.Args.Length > 0) throw new ArgumentException("Do not supply contructor arguments when mocking an interface or delegate."); try { instance = generator.CreateInterfaceProxyWithoutTarget(type, settings.AdditionalMockedInterfaces, options, interceptor); } catch (TypeLoadException ex) { proxyFailure = ex; } catch (GeneratorException ex) { proxyFailure = ex; } } else { try { #if SILVERLIGHT if (settings.Args == null || settings.Args.Length == 0) { ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (!constructors.Any(constr => constr.GetParameters().Length == 0)) { var constructorToCall = constructors.FirstOrDefault(); if (constructorToCall != null) { var @params = constructorToCall.GetParameters(); settings.Args = new object[@params.Length]; for (int i = 0; i < @params.Length; ++i) { var p = @params[i]; settings.Args[i] = Convert.IsDBNull(p.DefaultValue) ? p.ParameterType.GetDefaultValue() : p.DefaultValue; } } } } #endif instance = generator.CreateClassProxy(type, settings.AdditionalMockedInterfaces, options, settings.Args, interceptor); } catch (TypeLoadException ex) { proxyFailure = ex; } catch (GeneratorException ex) { proxyFailure = ex; } catch (InvalidProxyConstructorArgumentsException ex) { proxyFailure = ex; if (!settings.MockConstructorCall) throw new MockException(ex.Message); } } if (proxyFailure != null) { throw new ProxyFailureException(proxyFailure); } return instance; }
public IMockMixin CreateExternalMockMixin(IMockMixin mockMixin, IEnumerable<object> mixins) { var options = new ProxyGenerationOptions(); options.AddMixinInstance(mockMixin); foreach (var mixin in mixins) options.AddMixinInstance(mixin); var compoundMockMixin = (IMockMixin)generator.CreateClassProxy(typeof(MocksRepository.ExternalMockMixin), options); return compoundMockMixin; }