public static T CreateProxy <T>(this ProxyDefinition definition) { var builder = new ProxyBuilder(); var proxyType = builder.GetProxyType(definition); return((T)Activator.CreateInstance(proxyType)); }
private static Type CreateServiceProxyType(Type serviceType) { var proxyBuilder = new ProxyBuilder(); var proxyDefinition = CreateProxyDefinition(serviceType); ImplementServiceInterface(serviceType, proxyDefinition); return(proxyBuilder.GetProxyType(proxyDefinition)); }
/// <summary> /// Gets a proxy type that implements the <paramref name="serviceType"/>. /// </summary> /// <param name="serviceType">The type of service for which to create a service proxy type.</param> /// <returns>A proxy type that implements the <paramref name="serviceType"/>.</returns> public Type GetProxyType(Type serviceType) { var proxyDefinition = CreateProxyDefinition(serviceType); IProxyBuilder proxyBuilder = new ProxyBuilder(); return(proxyBuilder.GetProxyType(proxyDefinition)); }
public static T CreateLightInjectInterfaceProxy <T>(T target) { var proxyDefinition = new ProxyDefinition(typeof(T), () => target); proxyDefinition.Implement(() => new LightInjectInterceptor()); var proxyBuilder = new ProxyBuilder(); return((T)Activator.CreateInstance(proxyBuilder.GetProxyType(proxyDefinition))); }
public void GetProxyType_TypeAttributeWithStaticConstructor_ReturnsProxyWithClassAttribute() { var proxyDefinition = new ProxyDefinition(typeof(IMethodWithNoParameters)); proxyDefinition.AddCustomAttributes(typeof(ClassWithCustomAttributeWithStaticConstructor).GetCustomAttributesData().ToArray()); var proxyBuilder = new ProxyBuilder(); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); Assert.True(proxyType.IsDefined(typeof(CustomAttributeWithStaticConstructor), true)); }
public DynamicProxyCreationBenchmark() { var proxyBuilder = new ProxyBuilder(); var proxyGenerator = new ProxyGenerator(); var proxyFactory = new ProxyFactory(); _proxyType = proxyBuilder.GetProxyType(new ProxyDefinition(typeof(TestClass), true).Implement(() => new LightInjectInterceptor())); _dynamicProxyType = proxyGenerator.ProxyBuilder.CreateClassProxyType(typeof(TestClass), Type.EmptyTypes, ProxyGenerationOptions.Default); _nProxyInterceptor = new NProxyInterceptor(); _nproxyTemplate = proxyFactory.GetProxyTemplate(typeof(TestClass), Enumerable.Empty<Type>()); }
private T CreateProxy <T>(T target, IInterceptor interceptor) { ProxyBuilder proxyBuilder = new ProxyBuilder(); ProxyDefinition proxyDefinition = new ProxyDefinition(typeof(T), () => target); proxyDefinition.Implement(() => interceptor); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); var proxy = (T)Activator.CreateInstance(proxyType); return(proxy); }
public static void InitLightInject() { if (_createCalculator != null) { return; } _proxyType = _proxyBuilder.GetProxyType(new ProxyDefinition(typeof(Calculator), true).Implement(() => new LightInjectInterceptor())); var newExpression = Expression.New(_proxyType.GetConstructor(Type.EmptyTypes)); _createCalculator = Expression.Lambda <Func <ICalculator> >(newExpression).Compile(); }
public DynamicProxyCreationBenchmark() { var proxyBuilder = new ProxyBuilder(); var proxyGenerator = new ProxyGenerator(); var proxyFactory = new ProxyFactory(); _proxyType = proxyBuilder.GetProxyType(new ProxyDefinition(typeof(TestClass), true).Implement(() => new LightInjectInterceptor())); _dynamicProxyType = proxyGenerator.ProxyBuilder.CreateClassProxyType(typeof(TestClass), Type.EmptyTypes, ProxyGenerationOptions.Default); _nProxyInterceptor = new NProxyInterceptor(); _nproxyTemplate = proxyFactory.GetProxyTemplate(typeof(TestClass), Enumerable.Empty <Type>()); }
public void GetProxyType_TypeAttributeWithConstructorArgument_ReturnsProxyWithClassAttribute() { var proxyDefinition = new ProxyDefinition(typeof(IMethodWithNoParameters)); proxyDefinition.AddCustomAttributes(typeof(ClassWithCustomAttributeWithConstructorArgument).GetCustomAttributesData().ToArray()); var proxyBuilder = new ProxyBuilder(); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); var attribute = proxyType.GetCustomAttribute <CustomAttributeWithConstructorArgument>(); Assert.Equal(42, attribute.Value); }
public void Execute_AbstractMethod_InvokeInterceptor() { var interceptorMock = new Mock <IInterceptor>(); var proxyBuilder = new ProxyBuilder(); var proxyDefinition = new ProxyDefinition(typeof(ClassWithAbstractMethod), () => null); proxyDefinition.Implement(() => interceptorMock.Object); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); var instance = (ClassWithAbstractMethod)Activator.CreateInstance(proxyType); instance.Execute(); interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny <IInvocationInfo>()), Times.Once); }
public void GetProxyType_ClassWithProtectedConstructor_ImplementsPublicConstructor() { var interceptorMock = new Mock <IInterceptor>(); var proxyBuilder = new ProxyBuilder(); var proxyDefinition = new ProxyDefinition(typeof(FooWithProtectedConstructor), () => null); proxyDefinition.Implement(() => interceptorMock.Object); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); var instance = (FooWithProtectedConstructor)Activator.CreateInstance(proxyType); instance.Execute(); interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny <IInvocationInfo>()), Times.Once); //instance.Execute(); //interceptorMock.Verify(i => i.Invoke(It.IsAny<IInvocationInfo>()), Times.Once); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { if (serviceType == null) throw new ArgumentNullException("serviceType"); var attribute = serviceType.GetCustomAttributes(typeof(ServiceContractAttribute), true) .Cast<ServiceContractAttribute>() .FirstOrDefault(); if (!serviceType.IsInterface || attribute == null) throw new NotSupportedException("Only interfaces with [ServiceContract] attribute are supported with LightInjectServiceHostFactory."); var container = new ServiceContainer(); var proxyBuilder = new ProxyBuilder(); var proxyDefinition = new ProxyDefinition(serviceType, () => container.GetInstance(serviceType)); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); return base.CreateServiceHost(proxyType, baseAddresses); }
public void Execute_AbstractMethod_InvokeInterceptor() { var interceptorMock = new Mock<IInterceptor>(); var proxyBuilder = new ProxyBuilder(); var proxyDefinition = new ProxyDefinition(typeof(ClassWithAbstractMethod), () => null); proxyDefinition.Implement(() => interceptorMock.Object); var proxyType = proxyBuilder.GetProxyType(proxyDefinition); var instance = (ClassWithAbstractMethod)Activator.CreateInstance(proxyType); instance.Execute(); interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny<IInvocationInfo>()), Times.Once); }