public void ProxySpecificTargetTypeAttributeWithArrayProperty() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithArrayPropertyAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Spring.Proxy.IAnotherMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have 1 attribute applied to the target method."); Assert.AreEqual(1, attrs.Length, "Should have 1 attribute applied to the target method."); Assert.AreEqual(typeof(ArrayConstructorPropertyAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target method."); ArrayConstructorPropertyAttribute acpa = attrs[0] as ArrayConstructorPropertyAttribute; Assert.AreEqual(true, acpa.ReadOnly); Assert.AreEqual(2, acpa.Types.Length); Assert.AreEqual(typeof(int), acpa.Types[0]); Assert.AreEqual(typeof(string), acpa.Types[1]); }
public void ProxySpecificTargetTypeAttributeWithPropertySetterChangingDefaultBehavior() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithFakeXmlElementAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.GetParameters()[0].GetCustomAttributes(false); Assert.IsNotNull(method); Assert.IsNotNull(attrs, "Should have 1 attribute applied to the target method."); Assert.AreEqual(1, attrs.Length, "Should have 1 attribute applied to the target method."); Assert.AreEqual(typeof(FakeXmlElementAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target method."); FakeXmlElementAttribute xea = attrs[0] as FakeXmlElementAttribute; Assert.AreEqual(-1, xea.Order); }
public void OnClassThatDoesntImplementAnyInterfaces() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(DoesntImplementAnyInterfaces); builder.BuildProxyType(); }
public void AppliesSpecificMemberAttributesToSpecificMethodOnly() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(TargetObjectTest); IDictionary atts = new Hashtable(); atts.Add("InterfaceVirtualMethodWithNoArguments", new Attribute[] { new MarkerAttribute() }); builder.MemberAttributes = atts; Type proxy = builder.BuildProxyType(); object foo = Activator.CreateInstance(proxy); Type fooType = foo.GetType(); MethodInfo method = ReflectionUtils.GetMethod(fooType, "InterfaceMethodWithArguments", new Type[] { typeof(string) }); if (method == null) { method = ReflectionUtils.GetMethod(fooType, "Spring.Proxy.ITargetObjectTest.InterfaceMethodWithArguments", new Type[] { typeof(string) }); } object[] appliedAttributes = method.GetCustomAttributes(false); Assert.AreEqual(0, appliedAttributes.Length, "Custom attribute (erroneously) applied to member method."); method = ReflectionUtils.GetMethod(fooType, "InterfaceVirtualMethodWithNoArguments", Type.EmptyTypes); if (method == null) { method = ReflectionUtils.GetMethod(fooType, "Spring.Proxy.ITargetObjectTest.InterfaceVirtualMethodWithNoArguments", Type.EmptyTypes); } appliedAttributes = method.GetCustomAttributes(false); Assert.AreEqual(1, appliedAttributes.Length, "Custom attribute not applied to member method."); }
public void DoesNotGenerateProxyNameIfOneIsExplicitlySupplied() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.Name = "Bing"; Assert.AreEqual("Bing", builder.Name); }
/// <summary> /// 构造函数 /// </summary> /// <param name="typeBuilder">运行时类型构建器</param> /// <param name="proxyTypeBuilder">代理类型构建器</param> /// <param name="explicitImplementation">是否显示实现接口</param> /// <param name="targetMethods">目标对象方法字典</param> /// <param name="onProxyTargetMethods">代理目标对象的方法字典</param> public AbstractProxyMethodBuilder(TypeBuilder typeBuilder, IProxyTypeBuilder proxyTypeBuilder, bool explicitImplementation, IDictionary targetMethods, IDictionary onProxyTargetMethods) { this.typeBuilder = typeBuilder; this.proxyTypeBuilder = proxyTypeBuilder; this.explicitImplementation = explicitImplementation; this.targetMethods = targetMethods; this.onProxyTargetMethods = onProxyTargetMethods; }
protected virtual Type BuildProxyType(IProxyTypeBuilder typeBuilder) { ProxyTypeCacheKey cacheKey = new ProxyTypeCacheKey(typeBuilder.BaseType, typeBuilder.TargetType, typeBuilder.Interfaces, typeBuilder.ProxyTargetAttributes); if (!typeCache.TryGetValue(cacheKey, out Type proxyType)) { proxyType = typeBuilder.BuildProxyType(); typeCache.AddOrUpdate(cacheKey, proxyType, (k, old) => proxyType); //typeCache.TryAdd(cacheKey, proxyType); } return(proxyType); }
public void CheckInterfaceImplementation() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(MarkerClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is IMarkerInterface); Assert.IsFalse(foo is MarkerClass); }
public ServiceProxyBuilder( IServiceRegistry serviceRegistry, IProxyTypeBuilder proxyTypeBuilder, IProxyMethodExecutor proxyMethodExecutor, IAppServiceIocContainer appServiceIocContainer, ISerializedServiceProxyBuilder holder) { ((SerializedServiceProxyBuilderHolder)holder).Builder = this; _serviceRegistry = serviceRegistry; _proxyTypeBuilder = proxyTypeBuilder; _proxyMethodExecutor = proxyMethodExecutor; _appServiceIocContainer = appServiceIocContainer; }
public void ProxyTargetTypeAttributes() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(AnotherMarkerClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type."); }
public ServiceProxyBuilder( ICommunicationModelProvider communicationModelProvider, IProxyTypeBuilder proxyTypeBuilder, IProxyMethodExecutor proxyMethodExecutor, IDomainServiceProvider domainServiceProvider, ISerializedServiceProxyBuilder holder) { ((SerializedServiceProxyBuilderHolder)holder).Builder = this; _communicationModelProvider = communicationModelProvider; _proxyTypeBuilder = proxyTypeBuilder; _proxyMethodExecutor = proxyMethodExecutor; _domainServiceProvider = domainServiceProvider; }
public void CheckInheritType() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(DoesntImplementAnyInterfaces); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is DoesntImplementAnyInterfaces, "Using inheritance proxying, so the proxied object must inherit from the base type."); }
public void DoesNotProxyTargetTypeAttributesWithProxyTargetAttributesEqualsFalse() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(AnotherMarkerClass); builder.ProxyTargetAttributes = false; Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs); Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the target type."); }
public void ProxySecurityAttribute() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithSecurityAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 2 attribute applied to the target type."); Assert.AreEqual(2, attrs.Length, "Should have had 2 attribute applied to the target type."); }
public void DoesNotProxyTargetVirtualMethodReturnValueAttributesWithProxyTargetAttributesEqualsFalse() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(SomeMarkerClass); builder.ProxyTargetAttributes = false; Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("MarkerVirtualMethod"); Assert.IsNotNull(method); object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false); Assert.IsNotNull(attrs); Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the method's return value."); }
public void ProxyTargetVirtualMethodReturnValueAttributes() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(SomeMarkerClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("MarkerVirtualMethod"); Assert.IsNotNull(method); object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the method's return value."); Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the method's return value."); Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the method's return value."); }
public void DoesNotProxyNonProxiableInterface() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ApplicationClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is IApplicationInterface); Assert.IsFalse(foo is IFrameworkInterface); // try to call proxied interface methods ((IApplicationInterface)foo).ApplicationMethod(); }
public void OverrideVirtualMethod() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(TargetObjectTest); Type proxy = builder.BuildProxyType(); MethodInfo method = ReflectionUtils.GetMethod(proxy, "InterfaceVirtualMethodWithNoArguments", Type.EmptyTypes); Assert.IsNotNull(method, "Should define this method."); Assert.AreEqual(proxy, method.DeclaringType, "Method should be overrided in the proxy class."); // call overrided method object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is TargetObjectTest); ((TargetObjectTest)foo).InterfaceVirtualMethodWithNoArguments(); }
public void DoesNotImplementFinalMethodThatDoesNotImplementInterface() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(TargetObjectTest); Type proxy = builder.BuildProxyType(); MethodInfo method = ReflectionUtils.GetMethod(proxy, "MethodWithArguments", new Type[] { typeof(string) }); Assert.IsNotNull(method, "Should define this method."); Assert.AreNotEqual(proxy, method.DeclaringType, "Method can't be proxied."); // call base method object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is TargetObjectTest); ((TargetObjectTest)foo).MethodWithArguments("code"); }
public void ImplementFinalMethodThatImplementsInterface() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(TargetObjectTest); Type proxy = builder.BuildProxyType(); MethodInfo method = ReflectionUtils.GetMethod(proxy, "Spring.Proxy.ITargetObjectTest.InterfaceMethodWithArguments", new Type[] { typeof(string) }); Assert.IsNotNull(method, "Should define this method."); Assert.AreEqual(proxy, method.DeclaringType, "Method should be defined in the proxy class."); // call final interfaced method object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is ITargetObjectTest); ((ITargetObjectTest)foo).InterfaceMethodWithArguments("code"); }
public void SetsInterfacesToProxy() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(MultipleInterfaces); builder.Interfaces = new Type[] { typeof(IBase) }; Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is IBase); Assert.IsFalse(foo is IInherited); // try to call proxied interface methods ((IBase)foo).Base(); }
public void ProxySpecificTargetTypeAttributeNotInstantiableWithDefaultValues() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithFakeGenerateScriptTypeAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(typeof(FakeGenerateScriptTypeAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type."); FakeGenerateScriptTypeAttribute fgsta = attrs[0] as FakeGenerateScriptTypeAttribute; Assert.AreEqual("ScriptName", fgsta.Name); }
public void ProxySpecificTargetTypeAttributeWithReadOnlyProperty() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithGuidAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(typeof(GuidAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type."); GuidAttribute ga = attrs[0] as GuidAttribute; Assert.AreEqual("7cfc9607-e81a-48ac-a080-bda88193607b", ga.Value); }
public void DoesNotProxyTargetMethodParameterAttributesWithProxyTargetAttributesEqualsFalse() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(SomeMarkerClass); builder.ProxyTargetAttributes = false; Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.GetParameters()[1].GetCustomAttributes(false); Assert.IsNotNull(attrs); Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the method's parameter."); }
public void ProxyTargetMethodAttributes() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(AnotherMarkerClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Spring.Proxy.IAnotherMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have 1 attribute applied to the target method."); Assert.AreEqual(1, attrs.Length, "Should have 1 attribute applied to the target method."); Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target method."); }
public void ProxyWebServiceAttribute() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassWithWebServiceAttribute); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object[] attrs = proxy.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the target type."); Assert.AreEqual(typeof(WebServiceAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the target type."); WebServiceAttribute wsa = attrs[0] as WebServiceAttribute; Assert.AreEqual("blah", wsa.Name); Assert.AreEqual("http://mynamespace.com", wsa.Namespace); }
public void ProxyTargetMethodReturnValueMultipleAttributes() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(MultipleMarkerClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Oragon.Spring.Proxy.ISomeMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have had 2 attribute applied to the method's return value."); Assert.AreEqual(2, attrs.Length, "Should have had 2 attribute applied to the method's return value."); Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the method's return value."); Assert.AreEqual(typeof(MarkerAttribute), attrs[1].GetType(), "Wrong System.Type of Attribute applied to the method's return value."); }
public void DoesNotProxyNonProxiableInterface() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ApplicationClass); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); object foo = Activator.CreateInstance(proxy); Assert.IsTrue(foo is IApplicationInterface); Assert.IsTrue(foo is IFrameworkInterface); // proxy implements IApplicationInterface and proxy methods Assert.IsNotNull(foo.GetType().GetMethod("Spring.Proxy.IApplicationInterface.ApplicationMethod", BindingFlags.Instance | BindingFlags.NonPublic)); // proxy implements IFrameworkInterface but should not proxy methods Assert.IsNull(foo.GetType().GetMethod("Spring.Proxy.IFrameworkInterface.FrameworkMethod", BindingFlags.Instance | BindingFlags.NonPublic)); }
[Test] // SPRNET-1424 public void DoesNotProxyInterfaceMethodAttributes() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(object); builder.Interfaces = new Type[] { typeof(IAnotherMarkerInterface) }; Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); MethodInfo method = proxy.GetMethod("Oragon.Spring.Proxy.IAnotherMarkerInterface.MarkerMethod", BindingFlags.NonPublic | BindingFlags.Instance); if (method == null) { method = proxy.GetMethod("MarkerMethod"); } Assert.IsNotNull(method); object[] attrs = method.GetCustomAttributes(false); Assert.IsNotNull(attrs, "Should have 0 attribute applied to the target method."); Assert.AreEqual(0, attrs.Length, "Should have 0 attribute applied to the target method."); }
public void ProxyGenericInterface() { IProxyTypeBuilder builder = GetProxyBuilder(); builder.TargetType = typeof(ClassThatImplementsGenericInterface <TestObject>); Type proxy = builder.BuildProxyType(); Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to BuildProxy() was null."); GenericInterface <TestObject> foo = Activator.CreateInstance(proxy) as GenericInterface <TestObject>; Assert.IsNotNull(foo); TestObject to1 = foo.Create(); Assert.IsNotNull(to1); TestObject to2 = foo.Populate(new TestObject()); Assert.IsNotNull(to2); Assert.AreEqual("Populated", to2.Name); }
/// <summary> /// Generates the proxy type and caches the <see cref="System.Type"/> /// instance against the base type and the interfaces to proxy. /// </summary> /// <param name="typeBuilder"> /// The <see cref="Spring.Proxy.IProxyTypeBuilder"/> to use /// </param> /// <returns>The generated or cached proxy class.</returns> protected override Type BuildProxyType(IProxyTypeBuilder typeBuilder) { ProxyTypeCacheKey cacheKey = new ProxyTypeCacheKey( typeBuilder.BaseType, typeBuilder.TargetType, typeBuilder.Interfaces, typeBuilder.ProxyTargetAttributes); Type proxyType = null; lock (typeCache) { proxyType = typeCache[cacheKey] as Type; if (proxyType == null) { proxyType = typeBuilder.BuildProxyType(); typeCache[cacheKey] = proxyType; } else { #region Instrumentation if (logger.IsInfoEnabled) { logger.Info(String.Format( "AOP proxy type found in cache for '{0}'.", cacheKey)); } #endregion } } return proxyType; }
/// <summary> /// Generates the proxy type. /// </summary> /// <param name="typeBuilder"> /// The <see cref="Spring.Proxy.IProxyTypeBuilder"/> to use /// </param> /// <returns>The generated proxy class.</returns> protected virtual Type BuildProxyType(IProxyTypeBuilder typeBuilder) { return typeBuilder.BuildProxyType(); }