protected ProxyObjectReference(SerializationInfo info, StreamingContext context)
		{
			// Deserialize the base type using its assembly qualified name
			string qualifiedName = info.GetString("__baseType");
			_baseType = System.Type.GetType(qualifiedName, true, false);

			// Rebuild the list of interfaces
			var interfaceList = new List<System.Type>();
			int interfaceCount = info.GetInt32("__baseInterfaceCount");
			for (int i = 0; i < interfaceCount; i++)
			{
				string keyName = string.Format("__baseInterface{0}", i);
				string currentQualifiedName = info.GetString(keyName);
				System.Type interfaceType = System.Type.GetType(currentQualifiedName, true, false);

				interfaceList.Add(interfaceType);
			}

			// Reconstruct the proxy
			var factory = new ProxyFactory();
			System.Type proxyType = factory.CreateProxyType(_baseType, interfaceList.ToArray());

			// Initialize the proxy with the deserialized data
			var args = new object[] {info, context};
			_proxy = (IProxy) Activator.CreateInstance(proxyType, args);
		}
		public void CanProxyBasicGenericMethod()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			c.BasicGenericMethod<int>().Should().Be(5);
			c.BasicGenericMethod<string>().Should().Be("blha");
		}
		public void CanProxyBasicGenericMethod()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			Assert.That(c.BasicGenericMethod<int>(), Is.EqualTo(5));
			Assert.That(c.BasicGenericMethod<string>(), Is.EqualTo("blha"));
		}
		public void WhenProxyAnInterfaceShouldInterceptEquals()
		{
			var proxyFactory = new ProxyFactory();
			var interceptor = new InterceptedMethodsExposer();
			var proxy = proxyFactory.CreateProxy(typeof(IHasSomething), interceptor, null);
			proxy.Equals(null);
			Assert.That(interceptor.InterceptedMethods, Contains.Item("Equals"));
		}
		public void Proxy()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			var dictionary = new Dictionary<string, string>();
			var myParam = dictionary;
			c.Method(ref myParam);
			Assert.That(myParam, Is.Not.SameAs(dictionary));
		}
		public void Proxy()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			var dictionary = new Dictionary<string, string>();
			var myParam = dictionary;
			c.Method(ref myParam);
			myParam.Should().Not.Be.SameInstanceAs(dictionary);
		}
Ejemplo n.º 7
0
		public void Proxy()
		{
			var factory = new ProxyFactory();
			var c = (ClassWithVarietyOfMembers)factory.CreateProxy(typeof(ClassWithVarietyOfMembers), new PassThroughInterceptor(new ClassWithVarietyOfMembers()), null);

			int x;
			c.Method1(out x);
			Assert.AreEqual(3, x);

			x = 4;
			c.Method2(ref x);
			Assert.AreEqual(5, x);
		}
		public GeneratorProxyFactory(ProxyFactory factory, IDictionary<string, System.Type> proxies)
		{
			if (factory == null)
			{
				throw new ArgumentNullException("factory");
			}
			if (proxies == null)
			{
				throw new ArgumentNullException("proxies");
			}

			this.factory = factory;
			this.proxies = proxies;
		}
		public void CanProxyGenericMethodWithReferenceTypeAndInterfaceConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyDerivedClass()), null);
			c.MethodWithReferenceTypeAndInterfaceConstraint<MyDerivedClass>().Should().Not.Be.Null();
		}
		public void CanProxyGenericMethodWithInterfaceConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			c.MethodWithInterfaceConstraint<IMyInterface>(new MyDerivedClass()).Should().Be(typeof(IMyInterface));
		}
		public void CanProxyMethodWithDefaultConstructorConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			c.MethodWithConstructorConstraint<MyClass>().Should().Not.Be.Null();
		}
		public void CanProxySelfCastingMethod()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			Assert.That(c.As<MyClass>(), Is.Not.Null);
		}
		public void CanProxyMethodWithGenericBaseClassConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			c.MethodWithGenericBaseClassConstraint<MyGenericClass<int>, int>().Should().Be(typeof(MyGenericClass<int>));
		}
		public void CanProxySelfCastingMethodWithGenericConstaintBase2()
		{
			var factory = new ProxyFactory();
			var c = (MyGenericClass<int>) factory.CreateProxy(typeof (MyGenericClass<int>), new PassThroughInterceptor(new MyGenericClass<int>()), null);
			c.AsBase2<MyGenericClass<object>>().Should().Not.Be.Null();
		}
		public void CanProxySelfCastingMethodWithGenericConstaint2()
		{
			var factory = new ProxyFactory();
			var c = (MyGenericClass<int>) factory.CreateProxy(typeof (MyGenericClass<int>), new PassThroughInterceptor(new MyGenericClass<int>()), null);
			Assert.That(c.As2<MyGenericClass<object>>(), Is.Not.Null);
		}
		public void CanProxyGenericMethodWithInterfaceConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			Assert.That(c.MethodWithInterfaceConstraint<IMyInterface>(new MyDerivedClass()), Is.EqualTo(typeof(IMyInterface)));
		}
		public void CanProxySelfCastingMethod()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			c.As<MyClass>().Should().Not.Be.Null();
		}
		public void CanProxyMethodWithGenericBaseClassConstraint()
		{
			var factory = new ProxyFactory();
			var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
			Assert.That(c.MethodWithGenericBaseClassConstraint<MyGenericClass<int>, int>(), Is.EqualTo(typeof(MyGenericClass<int>)));
		}