Inheritance: IProxyFactory, IInitialize
示例#1
0
        /// <summary>
        /// Initializes a new instance of the ProxyObjectReference class.
        /// </summary>
        /// <param name="info">The <see cref="SerializationInfo"/> class that contains the serialized data.</param>
        /// <param name="context">The <see cref="StreamingContext"/> that describes the serialization state.</param>
        protected ProxyObjectReference(SerializationInfo info, StreamingContext context)
        {
            // Deserialize the base type using its assembly qualified name
            string qualifiedName = info.GetString("__baseType");
            _baseType = Type.GetType(qualifiedName, true, false);

            // Rebuild the list of interfaces
            List<Type> interfaceList = new List<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);
                Type interfaceType = Type.GetType(currentQualifiedName, true, false);

                interfaceList.Add(interfaceType);
            }

            // Reconstruct the proxy
            ProxyFactory factory = new ProxyFactory();
            Type proxyType = factory.CreateProxyType(_baseType, interfaceList.ToArray());
            _proxy = (IProxy)Activator.CreateInstance(proxyType);

            IInterceptor interceptor = (IInterceptor)info.GetValue("__interceptor", typeof(IInterceptor));
            _proxy.Interceptor = interceptor;
        }
示例#2
0
        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");
        }
示例#3
0
        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));
 }