public void How_to_create_a_dynamic_proxy() { var factory = new ProxyFactory(); var proxy = new PersonProxy(); var person = factory.CreateProxy<Person>(proxy as IInterceptor); person.Hello(); person.Greeting(); Console.WriteLine("RetValue: Foo"); }
void PlayWithCooperatingLibrary() { var coopViewModel = new CoopViewModel(); var proxyFactory = new ProxyFactory(); var proxy = proxyFactory.CreateProxy<CoopViewModel>(new ViewModelInvokeWrapper<CoopViewModel>(coopViewModel), typeof(IViewModel)); // When using coopViewModel directly, the bindings are updated correctly, when using the proxy the binding update breaks var view = new CoopView(coopViewModel /* proxy */); var win = new Window { Topmost = true, Content = view, SizeToContent = SizeToContent.WidthAndHeight }; win.Show(); }
public void ShouldSupportSerialization() { var factory = new ProxyFactory(); var interceptor = new SerializableInterceptor(); interceptor.Identifier = Guid.NewGuid(); var proxy = factory.CreateProxy<ISampleService>(interceptor); Type proxyType = proxy.GetType(); string proxyAssembly = proxyType.Assembly.Location; // The proxy type should have a default constructor // and a serialization constructor BindingFlags constructorFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; ConstructorInfo[] constructors = proxyType.GetConstructors(constructorFlags); Assert.IsTrue(constructors.Length == 2); ConstructorInfo serializationConstructor = proxyType.GetConstructor(constructorFlags, null, new[] { typeof (SerializationInfo), typeof (StreamingContext) }, null); Assert.IsNotNull(serializationConstructor); // Serialize the proxy var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, proxy); // Deserialize the proxy from the stream stream.Seek(0, SeekOrigin.Begin); var restoredProxy = (IProxy) formatter.Deserialize(stream); Assert.IsNotNull(restoredProxy); Assert.IsNotNull(restoredProxy.Interceptor); Assert.IsTrue(restoredProxy.Interceptor.GetType() == typeof (SerializableInterceptor)); var otherInterceptor = (SerializableInterceptor) restoredProxy.Interceptor; Assert.AreEqual(otherInterceptor.Identifier, interceptor.Identifier); }
MainViewModel CreateProxy(MainViewModel mainViewModel) { var proxyFactory = new ProxyFactory(); return proxyFactory.CreateProxy<MainViewModel>(new MainViewModelWrapper(mainViewModel)); }