private void InitializeInstance() { PexProtector.Invoke(() => { if (this.IsDelegateMock) { // We're mocking a delegate. // Firstly, get/create an interface with a method whose signature // matches that of the delegate. var delegateInterfaceType = proxyFactory.GetDelegateProxyInterface(typeof(T), out delegateInterfaceMethod); // Then create a proxy for that. var delegateProxy = proxyFactory.CreateProxy( delegateInterfaceType, this.Interceptor, this.ImplementedInterfaces.ToArray(), this.constructorArguments); // Then our instance is a delegate of the desired type, pointing at the // appropriate method on that proxied interface instance. this.instance = (T)(object)Delegate.CreateDelegate(typeof(T), delegateProxy, delegateInterfaceMethod); } else { this.instance = (T)proxyFactory.CreateProxy( typeof(T), this.Interceptor, this.ImplementedInterfaces.ToArray(), this.constructorArguments); } }); }