Esempio n. 1
0
        public void Mixin_With_InterfaceProxy_With_Target()
        {
            var generator = new ProxyGenerator();

            var options = new ProxyGeneratorOptions();

            options.AddMixinInstance(new PropertyChangedNotifier());

            var target = new MyClass_b();

            var proxy = generator.CreateInterfaceProxyWithTarget <IMyClass_b>(target, options);

            Assert.AreEqual(0, MyClass_b.States.Count);

            proxy.Title = "New title";

            Assert.AreEqual(1, MyClass_b.States.Count);
            Assert.AreEqual(StateTypes.Notify, MyClass_b.States[0]);

            MyClass_b.States.Clear();

            proxy.UpdateTitle(); // target.Title => out of the Proxy, cannot intercept

            Assert.AreEqual(0, MyClass_b.States.Count);
        }
        public void Get_Unique_Proxy_Name()
        {
            var moduleDefinition = new ModuleDefinition();

            var typeDefinition = moduleDefinition.GetTypeDefinition(typeof(TypeM1), null, null);

            var target          = typeof(TypeM1);
            var typeDefinition2 = moduleDefinition.GetTypeDefinition(typeof(TypeM1), target, null);

            var o = new ProxyGeneratorOptions();

            o.AddMixinInstance(new MyMixin());
            var typeDefinition3 = moduleDefinition.GetTypeDefinition(typeof(TypeM1), null, o);

            var typeDefinition4 = moduleDefinition.GetTypeDefinition(typeof(ITypeM1), target, null);

            var typeDefinition5 = moduleDefinition.GetTypeDefinition(typeof(ITypeM1), target, o);

            Assert.AreEqual(5, moduleDefinition.TypeDefinitions.Count);

            Assert.AreEqual(0, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition));
            Assert.AreEqual("NIntercept.Proxies.TypeM1_Proxy", typeDefinition.FullName);
            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition2));
            Assert.AreEqual("NIntercept.Proxies.TypeM1_1_Proxy", typeDefinition2.FullName);
            Assert.AreEqual(2, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition3));
            Assert.AreEqual("NIntercept.Proxies.TypeM1_2_Proxy", typeDefinition3.FullName);
            Assert.AreEqual(0, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition4));
            Assert.AreEqual("NIntercept.Proxies.ITypeM1_Proxy", typeDefinition4.FullName);
            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition5));
            Assert.AreEqual("NIntercept.Proxies.ITypeM1_1_Proxy", typeDefinition5.FullName);
        }
        public void Find_IndexOf()
        {
            var moduleDefinition = new ModuleDefinition();

            var typeDefinition = moduleDefinition.GetTypeDefinition(typeof(TypeM1), null, null);

            var target          = typeof(TypeM1);
            var typeDefinition2 = moduleDefinition.GetTypeDefinition(typeof(TypeM1), target, null);

            var o = new ProxyGeneratorOptions();

            o.AddMixinInstance(new MyMixin());
            var typeDefinition3 = moduleDefinition.GetTypeDefinition(typeof(TypeM1), null, o);

            var typeDefinition4 = moduleDefinition.GetTypeDefinition(typeof(ITypeM1), target, null);

            var typeDefinition5 = moduleDefinition.GetTypeDefinition(typeof(ITypeM1), target, o);

            Assert.AreEqual(5, moduleDefinition.TypeDefinitions.Count);

            Assert.AreEqual(0, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition));
            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition2));
            Assert.AreEqual(2, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition3));
            Assert.AreEqual(0, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition4));
            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.IndexOf(typeDefinition5));
        }
Esempio n. 4
0
        protected virtual void Initialize()
        {
            var options = new ProxyGeneratorOptions();

            options.AddMixinInstance(new PropertyChangedMixin());
            proxies.Add(typeof(MainWindowViewModel), generator.CreateClassProxy <MainWindowViewModel>(options));
        }
Esempio n. 5
0
        public void MixinNumberNotEquals()
        {
            var comparer = new ProxyGeneratorOptionsComparer();

            var o1 = new ProxyGeneratorOptions();

            o1.AddMixinInstance(new TypeITD());
            var o2 = new ProxyGeneratorOptions();

            Assert.AreEqual(false, comparer.Equals(o1, o2));
        }
Esempio n. 6
0
        public void SameMxinsTypeNotEquals()
        {
            var comparer = new ProxyGeneratorOptionsComparer();

            var o1 = new ProxyGeneratorOptions();

            o1.AddMixinInstance(new TypeITD());
            var o2 = new ProxyGeneratorOptions();

            o2.AddMixinInstance(new TypeITD());
            Assert.AreEqual(true, comparer.Equals(o1, o2));
        }
Esempio n. 7
0
        private static void RunMixinsSample(ProxyGenerator generator)
        {
            Console.WriteLine("----------------------           Mixins          ----------------------");

            var options = new ProxyGeneratorOptions();

            options.AddMixinInstance(new PropertyChangedMixin());

            var proxy = generator.CreateClassProxy <MyClassWithMixin>(options); // new LogInterceptor());

            proxy.Title = "New title";

            proxy.UpdateTitle(); // caution with ClassProxy and InterfaceProxy with targets, cannot work because the target is called, and this is not the property Title of the proxy that is called
        }
        public void Return_Same_TypeDefinition_With_Same_Type_And_Same_Target_Type_And_Same_Options()
        {
            var moduleDefinition = new ModuleDefinition();

            var target = typeof(TypeM1);
            var o      = new ProxyGeneratorOptions();

            o.AddMixinInstance(new MyMixin());

            var typeDefinition = moduleDefinition.GetTypeDefinition(typeof(TypeM1), target, o);

            Assert.AreEqual(TypeDefinitionType.ClassProxy, typeDefinition.TypeDefinitionType);
            Assert.AreEqual(typeof(ClassProxyDefinition), typeDefinition.GetType());
            Assert.AreEqual(typeof(TypeM1), typeDefinition.Type);
            Assert.AreEqual(target, typeDefinition.TargetType);

            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.Count);
            Assert.AreEqual(typeDefinition, moduleDefinition.TypeDefinitions[0]);

            // returns same type definition for same type with same target type or null and same options

            var target2 = typeof(TypeM1);
            var o2      = new ProxyGeneratorOptions();

            o2.AddMixinInstance(new MyMixin());

            var typeDefinition2 = moduleDefinition.GetTypeDefinition(typeof(TypeM1), target2, o2);

            Assert.AreEqual(TypeDefinitionType.ClassProxy, typeDefinition2.TypeDefinitionType);
            Assert.AreEqual(typeof(ClassProxyDefinition), typeDefinition2.GetType());
            Assert.AreEqual(typeof(TypeM1), typeDefinition2.Type);
            Assert.AreEqual(target2, typeDefinition2.TargetType);

            var comparer = new ProxyGeneratorOptionsComparer();

            Assert.AreEqual(true, comparer.Equals(o2, typeDefinition2.Options));


            Assert.AreEqual(typeDefinition2, typeDefinition);
            Assert.AreEqual(1, moduleDefinition.TypeDefinitions.Count);
            Assert.AreEqual(typeDefinition, moduleDefinition.TypeDefinitions[0]);
        }
Esempio n. 9
0
        public void Mixin_With_ClassProxy()
        {
            var generator = new ProxyGenerator();

            var options = new ProxyGeneratorOptions();

            options.AddMixinInstance(new PropertyChangedNotifier());

            var proxy = generator.CreateClassProxy <MyClass>(options);

            Assert.AreEqual(0, MyClass.States.Count);

            proxy.Title = "New title";

            Assert.AreEqual(1, MyClass.States.Count);
            Assert.AreEqual(StateTypes.Notify, MyClass.States[0]);

            MyClass.States.Clear();

            proxy.UpdateTitle();

            Assert.AreEqual(1, MyClass.States.Count);
            Assert.AreEqual(StateTypes.Notify, MyClass.States[0]);
        }
Esempio n. 10
0
        public void Mixin_Without_Interface_Throw()
        {
            var o = new ProxyGeneratorOptions();

            Assert.ThrowsException <ArgumentException>(() => o.AddMixinInstance(new InvalidMixin()));
        }
Esempio n. 11
0
        public void Mixin_Do_Not_Include_Doublons_Test()
        {
            var o = new ProxyGeneratorOptions();

            o.AddMixinInstance(new MyMixin());

            var moduleScope    = new ModuleScope();
            var generator      = new ProxyGenerator(moduleScope);
            var typeDefinition = generator.GetTypeDefinition(typeof(TypeScope1), null, o);

            TypeBuilder typeBulder = moduleScope.Module.DefineType(typeDefinition.FullName, typeDefinition.TypeAttributes);

            var proxyScope = new ProxyScope(moduleScope, typeBulder, typeDefinition);

            proxyScope.DefineTypeAndMembers();

            Assert.AreEqual(1, proxyScope.Properties.Count);
            Assert.AreEqual("MyProperty", proxyScope.Properties[0].Name);

            Assert.AreEqual(10, proxyScope.Methods.Count); // get set meth, add, remove
            Assert.AreEqual("get_MyProperty_Callback", proxyScope.Methods[0].Name);
            Assert.AreEqual("get_MyProperty", proxyScope.Methods[1].Name);
            Assert.AreEqual("set_MyProperty_Callback", proxyScope.Methods[2].Name);
            Assert.AreEqual("set_MyProperty", proxyScope.Methods[3].Name);
            Assert.AreEqual("Method_Callback", proxyScope.Methods[4].Name);
            Assert.AreEqual("Method", proxyScope.Methods[5].Name);
            Assert.AreEqual("add_MyEvent_Callback", proxyScope.Methods[6].Name);
            Assert.AreEqual("add_MyEvent", proxyScope.Methods[7].Name);
            Assert.AreEqual("remove_MyEvent_Callback", proxyScope.Methods[8].Name);
            Assert.AreEqual("remove_MyEvent", proxyScope.Methods[9].Name);

            Assert.AreEqual(1, proxyScope.Events.Count);
            Assert.AreEqual("MyEvent", proxyScope.Events[0].GetName());


            // fields
            Assert.AreEqual(2, proxyScope.ConstructorFields.Length);
            Assert.AreEqual("__interceptors", proxyScope.ConstructorFields[0].Name);
            Assert.AreEqual("__myMixin", proxyScope.ConstructorFields[1].Name);

            Assert.AreEqual(10, proxyScope.Fields.Count);
            Assert.AreEqual("__interceptors", proxyScope.Fields[0].Name);
            Assert.AreEqual("__myMixin", proxyScope.Fields[1].Name);
            Assert.AreEqual("TypeScope1_MyProperty", proxyScope.Fields[2].Name);
            Assert.AreEqual("TypeScope1_Proxy_get_MyProperty", proxyScope.Fields[3].Name);
            Assert.AreEqual("TypeScope1_Proxy_set_MyProperty", proxyScope.Fields[4].Name);

            Assert.AreEqual("TypeScope1_Method", proxyScope.Fields[5].Name);
            Assert.AreEqual("TypeScope1_Proxy_Method", proxyScope.Fields[6].Name);

            Assert.AreEqual("TypeScope1_MyEvent", proxyScope.Fields[7].Name);
            Assert.AreEqual("TypeScope1_Proxy_add_MyEvent", proxyScope.Fields[8].Name);
            Assert.AreEqual("TypeScope1_Proxy_remove_MyEvent", proxyScope.Fields[9].Name);

            // mappings
            var acc = new TypeAccessor(proxyScope);

            var propertyMappings = acc.Fields["propertyMappings"].GetValue() as List <PropertyMapping>;

            Assert.AreEqual(1, propertyMappings.Count);
            Assert.AreEqual("TypeScope1_MyProperty", propertyMappings[0].MemberField.Name);
            Assert.AreEqual("TypeScope1_Proxy_get_MyProperty", propertyMappings[0].GetMethodField.Name);
            Assert.AreEqual("TypeScope1_Proxy_set_MyProperty", propertyMappings[0].SetMethodField.Name);

            Assert.AreEqual("MyProperty", propertyMappings[0].Property.Name);
            Assert.AreEqual("get_MyProperty", propertyMappings[0].GetMethodBuilder.Name);
            Assert.AreEqual("set_MyProperty", propertyMappings[0].SetMethodBuilder.Name);

            var methodMappings = acc.Fields["methodMappings"].GetValue() as List <MethodMapping>;

            Assert.AreEqual(1, methodMappings.Count);
            Assert.AreEqual("TypeScope1_Method", methodMappings[0].MemberField.Name);
            Assert.AreEqual("TypeScope1_Proxy_Method", methodMappings[0].MethodField.Name);

            Assert.AreEqual("Method", methodMappings[0].Method.Name);
            Assert.AreEqual("Method", methodMappings[0].MethodBuilder.Name);

            var eventMappings = acc.Fields["eventMappings"].GetValue() as List <EventMapping>;

            Assert.AreEqual(1, eventMappings.Count);
            Assert.AreEqual("TypeScope1_MyEvent", eventMappings[0].MemberField.Name);
            Assert.AreEqual("TypeScope1_Proxy_add_MyEvent", eventMappings[0].AddMethodField.Name);
            Assert.AreEqual("TypeScope1_Proxy_remove_MyEvent", eventMappings[0].RemoveMethodField.Name);

            Assert.AreEqual("MyEvent", eventMappings[0].Event.Name);
            Assert.AreEqual("add_MyEvent", eventMappings[0].AddMethodBuilder.Name);
            Assert.AreEqual("remove_MyEvent", eventMappings[0].RemoveMethodBuilder.Name);
        }