public void XmlSerialization() { ProxyObjectReference.ResetScope(); GeneratorContext context = new GeneratorContext(); SimpleMixin mixin1 = new SimpleMixin(); OtherMixin mixin2 = new OtherMixin(); context.AddMixinInstance(mixin1); context.AddMixinInstance(mixin2); object proxy = generator.CreateCustomClassProxy( typeof(SimpleClass), new StandardInterceptor(), context); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(SimpleClass)); MemoryStream stream = new MemoryStream(); serializer.Serialize(stream, proxy); stream.Position = 0; SimpleClass otherProxy = (SimpleClass)serializer.Deserialize(stream); ISimpleMixin mixin = otherProxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); IOtherMixin other = otherProxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1, 2)); }
public void TwoMixins() { GeneratorContext context = new GeneratorContext(); SimpleMixin mixin1 = new SimpleMixin(); OtherMixin mixin2 = new OtherMixin(); context.AddMixinInstance( mixin1 ); context.AddMixinInstance( mixin2 ); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); object proxy = _generator.CreateCustomClassProxy( typeof(SimpleClass), interceptor, context ); Assert.IsFalse( interceptor.Invoked ); Assert.IsNotNull(proxy); Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) ); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.IsNotNull(mixin); Assert.AreEqual(1, mixin.DoSomething()); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin1, interceptor.mixin ); IOtherMixin other = proxy as IOtherMixin; Assert.IsNotNull(other); Assert.AreEqual(3, other.Sum(1,2)); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin2, interceptor.mixin ); }
public void SimpleMixin() { GeneratorContext context = new GeneratorContext(); SimpleMixin mixin_instance = new SimpleMixin(); context.AddMixinInstance(mixin_instance); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); object proxy = _generator.CreateCustomClassProxy( typeof(SimpleClass), interceptor, context); Assert.IsNotNull(proxy); Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType())); Assert.IsFalse(interceptor.Invoked); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.IsNotNull(mixin); Assert.AreEqual(1, mixin.DoSomething()); Assert.IsTrue(interceptor.Invoked); Assert.AreSame(proxy, interceptor.proxy); Assert.AreSame(mixin_instance, interceptor.mixin); }
protected override void CustomizeContext(GeneratorContext context, IKernel kernel, ComponentModel model, object[] arguments) { AspectDefinition aspect = (AspectDefinition)model.ExtendedProperties["aop.aspect"]; if (aspect == null) { return; } MixinDefinitionCollection mixins = aspect.Mixins; foreach (MixinDefinition definition in mixins) { Type mixinType = definition.TypeReference.ResolvedType; try { context.AddMixinInstance(Activator.CreateInstance(mixinType)); } catch (Exception e) { throw new ApplicationException("Could not instantiate mixin " + mixinType.FullName, e); } } }
public object RecreateInterfaceProxy(SerializationInfo info, StreamingContext context) { object proxy = null; GeneratorContext genContext = new GeneratorContext(); foreach (object mixin in _mixins) { genContext.AddMixinInstance(mixin); } InterfaceProxyGenerator gen = new InterfaceProxyGenerator(_scope, genContext); object target = info.GetValue("__target", typeof(object)); if (_mixins.Length == 0) { Type proxy_type = gen.GenerateCode(_interfaces, target.GetType()); proxy = Activator.CreateInstance(proxy_type, new object[] { _interceptor, target }); } else { Type proxy_type = gen.GenerateCode(_interfaces, target.GetType()); proxy = Activator.CreateInstance(proxy_type, new object[] { _interceptor, target, genContext.MixinsAsArray() }); } return(proxy); }
public object RecreateClassProxy(SerializationInfo info, StreamingContext context) { bool delegateBaseSer = info.GetBoolean("__delegateToBase"); if (!delegateBaseSer) { _data = (object[])info.GetValue("__data", typeof(object[])); } object proxy = null; GeneratorContext genContext = new GeneratorContext(); if (_mixins.Length != 0) { foreach (object mixin in _mixins) { genContext.AddMixinInstance(mixin); } } ClassProxyGenerator cpGen = new ClassProxyGenerator(_scope, genContext); Type proxy_type; if (_mixins.Length == 0) { proxy_type = cpGen.GenerateCode(_baseType, _interfaces); } else { proxy_type = cpGen.GenerateCustomCode(_baseType, _interfaces); } if (delegateBaseSer) { proxy = Activator.CreateInstance(proxy_type, new object[] { info, context }); } else { if (_mixins.Length == 0) { proxy = Activator.CreateInstance(proxy_type, new object[] { _interceptor }); } else { ArrayList args = new ArrayList(); args.Add(_interceptor); args.Add(genContext.MixinsAsArray()); proxy = Activator.CreateInstance(proxy_type, args.ToArray()); } MemberInfo[] members = FormatterServices.GetSerializableMembers(_baseType); FormatterServices.PopulateObjectMembers(proxy, members, _data); } return(proxy); }
/// <summary> /// Creates a context - which is used to guid custom proxy /// generation. /// </summary> /// <param name="mixins">Array of mixins to be registered</param> /// <returns>A GeneratorContext instance</returns> protected GeneratorContext CreateGeneratorContext(object[] mixins) { GeneratorContext context = new GeneratorContext(); foreach (object mixin in mixins) { context.AddMixinInstance(mixin); } return(context); }
public void MixinSerialization() { ProxyObjectReference.ResetScope(); GeneratorContext context = new GeneratorContext(); SimpleMixin mixin1 = new SimpleMixin(); OtherMixin mixin2 = new OtherMixin(); context.AddMixinInstance(mixin1); context.AddMixinInstance(mixin2); object proxy = generator.CreateCustomClassProxy( typeof(SimpleClass), new StandardInterceptor(), context); Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType())); (proxy as SimpleClass).DoSome(); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); IOtherMixin other = proxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1, 2)); SimpleClass otherProxy = (SimpleClass)SerializeAndDeserialize(proxy); otherProxy.DoSome(); mixin = otherProxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); other = otherProxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1, 2)); }
public object RecreateClassProxy(SerializationInfo info, StreamingContext context) { Type type; bool boolean = info.GetBoolean("__delegateToBase"); if (!boolean) { this._data = (object[])info.GetValue("__data", typeof(object[])); } object obj2 = null; GeneratorContext context2 = new GeneratorContext(); if (this._mixins.Length != 0) { foreach (object obj3 in this._mixins) { context2.AddMixinInstance(obj3); } } ClassProxyGenerator generator = new ClassProxyGenerator(_scope, context2); if (this._mixins.Length == 0) { type = generator.GenerateCode(this._baseType, this._interfaces); } else { type = generator.GenerateCustomCode(this._baseType, this._interfaces); } if (boolean) { return(Activator.CreateInstance(type, new object[] { info, context })); } if (this._mixins.Length == 0) { obj2 = Activator.CreateInstance(type, new object[] { this._interceptor }); } else { ArrayList list = new ArrayList(); list.Add(this._interceptor); list.Add(context2.MixinsAsArray()); obj2 = Activator.CreateInstance(type, list.ToArray()); } MemberInfo[] serializableMembers = FormatterServices.GetSerializableMembers(this._baseType); FormatterServices.PopulateObjectMembers(obj2, serializableMembers, this._data); return(obj2); }
public object RecreateInterfaceProxy(SerializationInfo info, StreamingContext context) { GeneratorContext context2 = new GeneratorContext(); foreach (object obj3 in this._mixins) { context2.AddMixinInstance(obj3); } InterfaceProxyGenerator generator = new InterfaceProxyGenerator(_scope, context2); object obj4 = info.GetValue("__target", typeof(object)); if (this._mixins.Length == 0) { return(Activator.CreateInstance(generator.GenerateCode(this._interfaces, obj4.GetType()), new object[] { this._interceptor, obj4 })); } return(Activator.CreateInstance(generator.GenerateCode(this._interfaces, obj4.GetType()), new object[] { this._interceptor, obj4, context2.MixinsAsArray() })); }
public void MixinImplementingMoreThanOneInterface() { GeneratorContext context = new GeneratorContext(); ComplexMixin mixin_instance = new ComplexMixin(); context.AddMixinInstance(mixin_instance); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); object proxy = _generator.CreateCustomClassProxy( typeof(SimpleClass), interceptor, context); Assert.IsNotNull(proxy); Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType())); Assert.IsFalse(interceptor.Invoked); IThird inter3 = proxy as IThird; Assert.IsNotNull(inter3); inter3.DoThird(); Assert.IsTrue(interceptor.Invoked); Assert.AreSame(proxy, interceptor.proxy); Assert.AreSame(mixin_instance, interceptor.mixin); ISecond inter2 = proxy as ISecond; Assert.IsNotNull(inter2); inter2.DoSecond(); Assert.IsTrue(interceptor.Invoked); Assert.AreSame(proxy, interceptor.proxy); Assert.AreSame(mixin_instance, interceptor.mixin); IFirst inter1 = proxy as IFirst; Assert.IsNotNull(inter1); inter1.DoFirst(); Assert.IsTrue(interceptor.Invoked); Assert.AreSame(proxy, interceptor.proxy); Assert.AreSame(mixin_instance, interceptor.mixin); }
/// <summary> /// Creates a proxy for an XML-RPC service. /// </summary> /// <typeparam name="T">XML-RPC service interface.</typeparam> /// <returns></returns> public T CreateProxy <T>() { // // First, create mixin XmlRpcServiceProxy mixin = new XmlRpcServiceProxy(typeof(T)); mixin.WebRequestFactory = new HttpXmlRpcWebRequestFactory(); // // Now create implementation GeneratorContext generatorContext = new GeneratorContext(); generatorContext.AddMixinInstance(mixin); ProxyGenerator proxyGenerator = new ProxyGenerator(); return((T)proxyGenerator.CreateCustomProxy(typeof(T), new XmlRpcServiceProxyInterceptor(), mixin, generatorContext)); }
public void MixinForInterfaces() { GeneratorContext context = new GeneratorContext(); SimpleMixin mixin_instance = new SimpleMixin(); context.AddMixinInstance( mixin_instance ); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); MyInterfaceImpl target = new MyInterfaceImpl(); object proxy = _generator.CreateCustomProxy( typeof(IMyInterface), interceptor, target, context ); Assert.IsNotNull(proxy); Assert.IsTrue( typeof(IMyInterface).IsAssignableFrom( proxy.GetType() ) ); Assert.IsFalse( interceptor.Invoked ); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.IsNotNull(mixin); Assert.AreEqual(1, mixin.DoSomething()); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin_instance, interceptor.mixin ); }
public void MixinImplementingMoreThanOneInterface() { GeneratorContext context = new GeneratorContext(); ComplexMixin mixin_instance = new ComplexMixin(); context.AddMixinInstance( mixin_instance ); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); object proxy = _generator.CreateCustomClassProxy( typeof(SimpleClass), interceptor, context ); Assert.IsNotNull(proxy); Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) ); Assert.IsFalse( interceptor.Invoked ); IThird inter3 = proxy as IThird; Assert.IsNotNull(inter3); inter3.DoThird(); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin_instance, interceptor.mixin ); ISecond inter2 = proxy as ISecond; Assert.IsNotNull(inter2); inter2.DoSecond(); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin_instance, interceptor.mixin ); IFirst inter1 = proxy as IFirst; Assert.IsNotNull(inter1); inter1.DoFirst(); Assert.IsTrue( interceptor.Invoked ); Assert.AreSame( proxy, interceptor.proxy ); Assert.AreSame( mixin_instance, interceptor.mixin ); }
public object RecreateInterfaceProxy(SerializationInfo info, StreamingContext context) { object proxy = null; GeneratorContext genContext = new GeneratorContext(); foreach(object mixin in _mixins) { genContext.AddMixinInstance(mixin); } InterfaceProxyGenerator gen = new InterfaceProxyGenerator( _scope, genContext ); object target = info.GetValue("__target", typeof(object)); if (_mixins.Length == 0) { Type proxy_type = gen.GenerateCode( _interfaces, target.GetType()); proxy = Activator.CreateInstance( proxy_type, new object[] { _interceptor, target } ); } else { Type proxy_type = gen.GenerateCode( _interfaces, target.GetType() ); proxy = Activator.CreateInstance( proxy_type, new object[] { _interceptor, target, genContext.MixinsAsArray() } ); } return proxy; }
public object RecreateClassProxy(SerializationInfo info, StreamingContext context) { bool delegateBaseSer = info.GetBoolean("__delegateToBase"); if (!delegateBaseSer) { _data = (object[]) info.GetValue("__data", typeof(object[]) ); } object proxy = null; GeneratorContext genContext = new GeneratorContext(); if (_mixins.Length != 0) { foreach(object mixin in _mixins) { genContext.AddMixinInstance(mixin); } } ClassProxyGenerator cpGen = new ClassProxyGenerator( _scope, genContext ); Type proxy_type; if (_mixins.Length == 0) { proxy_type = cpGen.GenerateCode( _baseType, _interfaces ); } else { proxy_type = cpGen.GenerateCustomCode( _baseType, _interfaces ); } if (delegateBaseSer) { proxy = Activator.CreateInstance( proxy_type, new object[] { info, context } ); } else { if (_mixins.Length == 0) { proxy = Activator.CreateInstance( proxy_type, new object[] { _interceptor } ); } else { ArrayList args = new ArrayList(); args.Add(_interceptor); args.Add(genContext.MixinsAsArray()); proxy = Activator.CreateInstance( proxy_type, args.ToArray() ); } MemberInfo[] members = FormatterServices.GetSerializableMembers( _baseType ); FormatterServices.PopulateObjectMembers(proxy, members, _data); } return proxy; }
public void XmlSerialization() { ProxyObjectReference.ResetScope(); GeneratorContext context = new GeneratorContext(); SimpleMixin mixin1 = new SimpleMixin(); OtherMixin mixin2 = new OtherMixin(); context.AddMixinInstance( mixin1 ); context.AddMixinInstance( mixin2 ); object proxy = generator.CreateCustomClassProxy( typeof(SimpleClass), new StandardInterceptor(), context ); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(SimpleClass)); MemoryStream stream = new MemoryStream(); serializer.Serialize(stream, proxy); stream.Position = 0; SimpleClass otherProxy = (SimpleClass) serializer.Deserialize( stream ); ISimpleMixin mixin = otherProxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); IOtherMixin other = otherProxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1,2)); }
public void MixinSerialization() { ProxyObjectReference.ResetScope(); GeneratorContext context = new GeneratorContext(); SimpleMixin mixin1 = new SimpleMixin(); OtherMixin mixin2 = new OtherMixin(); context.AddMixinInstance( mixin1 ); context.AddMixinInstance( mixin2 ); object proxy = generator.CreateCustomClassProxy( typeof(SimpleClass), new StandardInterceptor(), context ); Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) ); (proxy as SimpleClass).DoSome(); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); IOtherMixin other = proxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1,2)); SimpleClass otherProxy = (SimpleClass) SerializeAndDeserialize(proxy); otherProxy.DoSome(); mixin = otherProxy as ISimpleMixin; Assert.AreEqual(1, mixin.DoSomething()); other = otherProxy as IOtherMixin; Assert.AreEqual(3, other.Sum(1,2)); }