コード例 #1
0
        public void DynamicTypeBuilderTransactionalProxyHasMultipleValues()
        {
            var target1 = new TransactionProxyClass
            {
                DecimalProperty = 123,
                IntProperty     = 456,
                StringProperty  = "Test",
                Targets         = new Collection <TestTarget> {
                    new TestTarget()
                }
            };

            var target2 = new TransactionProxyClass
            {
                DecimalProperty = 123,
                IntProperty     = 457,
                StringProperty  = "Test",
                Targets         = new Collection <TestTarget> {
                    new TestTarget()
                }
            };

            var target3 = new TransactionProxyClass
            {
                DecimalProperty = 123,
                IntProperty     = 458,
                StringProperty  = "Test3",
                Targets         = new Collection <TestTarget> {
                    new TestTarget()
                }
            };

            var dtb = new DynamicTypeBuilder <TransactionProxyClass>();

            dtb.AddConvention(new TransactionProxyConvention(typeof(TransactionProxyClass), true));
            if (!dtb.Conventions.OfType <NotifyPropertyChangedConvention>().Any())
            {
                dtb.AddConvention(new NotifyPropertyChangedConvention());
            }

            var proxy = Activator.CreateInstance(dtb.GeneratedType) as TransactionProxyClass;

            ((ITransactionProxy <TransactionProxyClass>)proxy).SetTargets(new[] { target1, target2, target3 });

            var type = proxy.GetType();

            Assert.False((bool)type.GetProperty("DecimalProperty_HasMultipleValues").GetValue(proxy));
            Assert.True((bool)type.GetProperty("IntProperty_HasMultipleValues").GetValue(proxy));
            Assert.True((bool)type.GetProperty("StringProperty_HasMultipleValues").GetValue(proxy));
            Assert.True((bool)type.GetProperty("Targets_HasMultipleValues").GetValue(proxy));
        }
コード例 #2
0
        public void DynamicTypeBuilderSimplePropertyTest()
        {
            var props = new[] { "Key", "Category", "de", "en", "fr", "es", "ru", "zh", "ar" };

            var members = props.Select(p => new DynamicProperty(p, typeof(string)));

            var dtb = new DynamicTypeBuilder(members);

            dtb.AddConvention(new NotifyPropertyChangedConvention());

            var instance = Activator.CreateInstance(dtb.GeneratedType);

            var host = (IDynamicPropertyHost)instance;

            host.SetValue("Key", "123");

            host.SetValue("Category", "Category");
            host.SetValue("de", "loc_de");
            host.SetValue("en", "loc_en");
            host.SetValue("fr", "loc_fr");
            host.SetValue("ru", "loc_ru");
            host.SetValue("es", "loc_es");
            host.SetValue("zh", "loc_zh");
            host.SetValue("ar", "loc_ar");

            Assert.Equal("loc_de", host.GetValue("de"));
            Assert.Equal("loc_en", host.GetValue("en"));
            Assert.Equal("loc_fr", host.GetValue("fr"));
            Assert.Equal("loc_ru", host.GetValue("ru"));
            Assert.Equal("loc_es", host.GetValue("es"));
            Assert.Equal("loc_zh", host.GetValue("zh"));
            Assert.Equal("loc_ar", host.GetValue("ar"));
        }
コード例 #3
0
ファイル: ScopeProxyTest.cs プロジェクト: codeworxOS/Lucile
        public void TestSyncMethodsProxy()
        {
            var serviceCollection = new ServiceCollection();

            var dtb = new DynamicTypeBuilder <ScopeProxy <ISyncService> >();

            dtb.AddConvention(new ProxyConvention <ISyncService>());

            serviceCollection.AddConnectedService <ISyncService>();
            serviceCollection.AddConnectedLocal <ISyncService, SyncService>();
            serviceCollection.AddScoped <ScopedDependency>();
            serviceCollection.AddScopeProxy();

            var prov    = serviceCollection.BuildServiceProvider();
            var service = prov.GetRequiredService <ISyncService>();

            var text = Guid.NewGuid().ToString();
            var call = service.SyncMethodWithResult(text, 123);

            Assert.Equal(text, call);

            var ex  = Assert.Throws <SyncService.SyncServiceException>(() => service.SyncVoidMethod(text, 1234));
            var ex2 = Assert.Throws <SyncService.SyncServiceException>(() => service.SyncVoidMethod(text, 1234));

            Assert.NotEqual(ex.ScopedDependency.Id, ex2.ScopedDependency.Id);
        }
コード例 #4
0
        public void DynamicTypeBuilderTransactionalProxySingleCommit()
        {
            var target1 = new TransactionProxyClass
            {
                DecimalProperty = 123,
                IntProperty     = 456,
                StringProperty  = "Test",
                Targets         = new Collection <TestTarget> {
                    new TestTarget()
                }
            };

            var dtb = new DynamicTypeBuilder <TransactionProxyClass>();

            dtb.AddConvention(new TransactionProxyConvention(typeof(TransactionProxyClass), true));
            if (!dtb.Conventions.OfType <NotifyPropertyChangedConvention>().Any())
            {
                dtb.AddConvention(new NotifyPropertyChangedConvention());
            }

            var proxy = Activator.CreateInstance(dtb.GeneratedType) as TransactionProxyClass;

            ((ITransactionProxy <TransactionProxyClass>)proxy).SetTargets(new[] { target1 });

            Assert.Equal(123, proxy.DecimalProperty);
            Assert.Equal(456, proxy.IntProperty);
            Assert.Equal("Test", proxy.StringProperty);
            Assert.Single(proxy.Targets);
            Assert.Equal(target1.Targets.First(), proxy.Targets.First());

            proxy.DecimalProperty = 1234;
            proxy.IntProperty     = 5678;
            proxy.StringProperty  = "Test2";
            proxy.Targets.Add(new TestTarget());

            Assert.Equal(123, target1.DecimalProperty);
            Assert.Equal(456, target1.IntProperty);
            Assert.Equal("Test", target1.StringProperty);
            Assert.Single(target1.Targets);

            ((ICommitable)proxy).Commit();

            Assert.Equal(1234, target1.DecimalProperty);
            Assert.Equal(5678, target1.IntProperty);
            Assert.Equal("Test2", target1.StringProperty);
            Assert.Equal(2, target1.Targets.Count());
        }
コード例 #5
0
        public void InterceptionContextProxyInterceptionTest()
        {
            var  instance = new SimpleMethods();
            bool beforeStringCalled = false, afterStringCalled = false, insteadOfStringCalled = false;

            object[] arguments = null;

            var dtb = new DynamicTypeBuilder <DynamicProxyBase>();

            dtb.AddConvention(new ProxyConvention <ISimpleMethods>());

            var proxy = Activator.CreateInstance(dtb.GeneratedType) as ISimpleMethods;

            //var dtb2 = new DynamicTypeBuilder<DynamicProxyBase>();
            //dtb2.AddMember(new DynamicProperty("StringProperty", typeof(string)));
            //var proxy2 = Activator.CreateInstance(dtb2.GeneratedType) as ISimpleMethods;

            ((IDynamicProxy)proxy).SetProxyTarget <ISimpleMethods>(instance);

            ((DynamicProxyBase)proxy).InterceptorCalled += (m, t) =>
            {
                if (m == InterceptionMode.BeforeBody && t.MemberName == "StringWithParameters")
                {
                    beforeStringCalled = true;
                    arguments          = t.Arguments;
                }
                if (m == InterceptionMode.AfterBody && t.MemberName == "StringWithParameters")
                {
                    afterStringCalled = true;
                }
                if (m == InterceptionMode.InsteadOfBody && t.MemberName == "StringWithParameters")
                {
                    insteadOfStringCalled = true;
                }
            };

            var result = proxy.StringWithParameters("TestString", 1, null);

            proxy.Void();
            proxy.VoidWithParameters("test", 123, null);
            var intTest = proxy.Integer();

            proxy.String();
            proxy.TestClass();
            proxy.TestClassWithParameters("test", 2, null);
            var generic  = proxy.GenericText <int>(123, "test");
            var generic2 = proxy.GenericText <string>("testchen", "test");

            Assert.Equal(123, generic);
            Assert.Equal("testchen", generic2);

            Assert.True(beforeStringCalled);
            Assert.True(afterStringCalled);
            Assert.True(insteadOfStringCalled);

            Assert.Equal(3, arguments.Length);
            Assert.Equal("TestString", arguments[0]);
            Assert.Equal("TestString", result);
        }
コード例 #6
0
        public void DynamicTypeBuilderTransactionalProxyInterfaceTest()
        {
            var dtb = new DynamicTypeBuilder <TransactionProxyClass>();

            dtb.AddConvention(new TransactionProxyConvention(typeof(TransactionProxyClass), true));
            if (!dtb.Conventions.OfType <NotifyPropertyChangedConvention>().Any())
            {
                dtb.AddConvention(new NotifyPropertyChangedConvention());
            }

            var proxy = Activator.CreateInstance(dtb.GeneratedType) as TransactionProxyClass;

            Assert.True(proxy is ICommitable);
            Assert.True(proxy is ITransactionProxy);
            Assert.True(proxy is INotifyPropertyChanged);
            Assert.True(proxy is ITransactionProxy <TransactionProxyClass>);
        }
コード例 #7
0
#pragma warning restore RECS0108 // Warns about static fields in generic types

        static ServiceScopeProxy()
        {
            var dtb = new DynamicTypeBuilder <ScopeProxy <TService> >(assemblyBuilderFactory: ScopeProxyHelper.AssemblyBuilderFactory);

            dtb.AddConvention(new ProxyConvention <TService>());
            _proxyType = dtb.GeneratedType;

            var param = Expression.Parameter(typeof(IServiceProvider), "p");
            var body  = Expression.New(_proxyType.GetConstructor(new[] { typeof(IServiceProvider) }), param);

            _createDelegate = Expression.Lambda <Func <IServiceProvider, TService> >(body, param).Compile();
        }
コード例 #8
0
        public void DynamicTypeBuilderTransactionalProxySetTypedTarget()
        {
            var target1 = new TransactionProxyClass();
            var target2 = new TransactionProxyClass();

            var dtb = new DynamicTypeBuilder <TransactionProxyClass>();

            dtb.AddConvention(new TransactionProxyConvention(typeof(TransactionProxyClass), true));
            if (!dtb.Conventions.OfType <NotifyPropertyChangedConvention>().Any())
            {
                dtb.AddConvention(new NotifyPropertyChangedConvention());
            }

            var proxy = Activator.CreateInstance(dtb.GeneratedType) as ITransactionProxy <TransactionProxyClass>;

            proxy.SetTargets(new[] { target1 });
            Assert.Single(proxy.Targets);
            Assert.Equal(target1, proxy.Targets.First());

            proxy.SetTargets(new[] { target2 });
            Assert.Single(proxy.Targets);
            Assert.Equal(target2, proxy.Targets.First());
        }
コード例 #9
0
        public void DynamicTypeBuilderPropertyNewNotificationOverride()
        {
            var dtb = new DynamicTypeBuilder <NonVirtualNotificationBaseProperties>();

            dtb.AddMember(new DynamicProperty("StringProperty", typeof(string)));
            dtb.AddMember(new DynamicProperty("IntProperty", typeof(int)));
            dtb.AddMember(new DynamicProperty("ByteArrayProperty", typeof(byte[])));
            dtb.AddMember(new DynamicProperty("BlaProperty", typeof(string)));

            dtb.AddConvention(new NotifyPropertyChangedConvention());

            var type     = dtb.GeneratedType;
            var instance = Activator.CreateInstance(type);

            Assert.NotNull(instance);

            string changedProperty = null;

            ((INotifyPropertyChanged)instance).PropertyChanged += (s, ea) => changedProperty = ea.PropertyName;

            type.GetProperties().First(p => p.Name == "StringProperty" && p.DeclaringType == type).SetValue(instance, "Bla");
            Assert.Equal("StringProperty", changedProperty);

            changedProperty = null;
            ((NonVirtualNotificationBaseProperties)instance).StringProperty = "Bla2";
            Assert.Equal("StringProperty", changedProperty);

            changedProperty = null;
            type.GetProperties().First(p => p.Name == "IntProperty" && p.DeclaringType == type).SetValue(instance, 123);
            Assert.Equal("IntProperty", changedProperty);

            changedProperty = null;
            ((NonVirtualNotificationBaseProperties)instance).IntProperty = 456;
            Assert.Equal("IntProperty", changedProperty);

            changedProperty = null;
            type.GetProperties().First(p => p.Name == "ByteArrayProperty" && p.DeclaringType == type).SetValue(instance, new byte[] { 1, 2, 3 });
            Assert.Equal("ByteArrayProperty", changedProperty);

            changedProperty = null;
            ((NonVirtualNotificationBaseProperties)instance).ByteArrayProperty = new byte[] { 4, 5, 6 };
            Assert.Equal("ByteArrayProperty", changedProperty);

            changedProperty = null;
            type.GetProperties().First(p => p.Name == "BlaProperty" && p.DeclaringType == type).SetValue(instance, "test");
            Assert.Equal("BlaProperty", changedProperty);
        }
コード例 #10
0
        public void TestDynamicPropertyNotification()
        {
            DynamicTypeBuilder builder = new DynamicTypeBuilder(
                new DynamicMember[] {
                new DynamicProperty("StringProperty", typeof(string)),
                new DynamicProperty("DecimalProperty", typeof(decimal)),
                new DynamicProperty("NullableDecimalProperty", typeof(decimal?)),
                new DynamicProperty("DateTimeProperty", typeof(DateTime)),
                new DynamicProperty("TestClassProperty", typeof(TestClass))
            }
                );

            builder.AddConvention(new NotifyPropertyChangedConvention());

            var instance = Activator.CreateInstance(builder.GeneratedType);

            Assert.True(typeof(INotifyPropertyChanged).IsInstanceOfType(instance));

            List <string> propertyNames = new List <string>();

            ((INotifyPropertyChanged)instance).PropertyChanged += (s, ea) => { propertyNames.Add(ea.PropertyName); };

            var raise = builder.GeneratedType.GetMethod("RaisePropertyChanged", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.NotNull(raise);
            raise.Invoke(instance, new object[] { "IntProperty" });

            Assert.Single(propertyNames);
            Assert.Equal("IntProperty", propertyNames[0]);

            ((IDynamicPropertyHost)instance).SetValue("StringProperty", "TestString");

            Assert.Equal(2, propertyNames.Count);
            Assert.Equal("StringProperty", propertyNames[1]);

            ((IDynamicPropertyHost)instance).SetValue("NullableDecimalProperty", 4.3m);

            Assert.Equal(3, propertyNames.Count);
            Assert.Equal("NullableDecimalProperty", propertyNames[2]);
        }
コード例 #11
0
        public ChannelProxyCacheItem(AssemblyBuilderFactory assemblyFactory)
        {
            var serviceContract = typeof(TChannel).GetCustomAttributes(typeof(ServiceContractAttribute), true).OfType <ServiceContractAttribute>().FirstOrDefault();

            if (serviceContract != null)
            {
                this.HasCallback  = serviceContract.CallbackContract != null;
                this.CallbackType = serviceContract.CallbackContract;
            }

            var members = new List <DynamicMember>();

            if (HasCallback)
            {
                foreach (var item in CallbackType.GetInterfaces().Union(new[] { CallbackType }))
                {
                    if (item.GetProperties().Any() ||
                        item.GetEvents().Any() ||
                        item.GetMethods().Where(p => p.ReturnType != typeof(void)).Any())
                    {
                        throw new InvalidOperationException("Only methods and only void retrun types are allowed for callback contracts.");
                    }

                    members.AddRange(item.GetMethods().Select(p => new DynamicVoid(p.Name, p.GetParameters().Select(x => x.ParameterType).ToArray())));
                }
            }

            var dtb = new DynamicTypeBuilder <TProxy>(members, assemblyFactory);

            dtb.AddConvention(new ProxyConvention <TChannel>());
            if (HasCallback)
            {
                dtb.AddInterceptor(new ImplementInterfaceInterceptor(this.CallbackType));
            }

            this.proxyType = dtb.GeneratedType;

            createProxyDelegate = Expression.Lambda <Func <TProxy> >(Expression.New(this.proxyType)).Compile();
        }
コード例 #12
0
ファイル: DynamicProxyTest.cs プロジェクト: codeworxOS/Lucile
        public void DynamicProxyTestSimpleMethods()
        {
            var simpleMethods = new SimpleMethods();

            string lastCalled = string.Empty;

            simpleMethods.GotCalled += (s, e) => lastCalled = e.MemberName;

            var dtb = new DynamicTypeBuilder();

            dtb.AddConvention(new ProxyConvention <ISimpleMethods>());
            var inst = Activator.CreateInstance(dtb.GeneratedType) as ISimpleMethods;

            Assert.NotNull(inst);
            Assert.True(inst is IDynamicProxy);

            ((IDynamicProxy)inst).SetProxyTarget <ISimpleMethods>(simpleMethods);

            inst.Void();
            Assert.Equal("Void", lastCalled);

            inst.VoidWithParameters("Void", 0, new TestClass());
            Assert.Equal("VoidWithParameters", lastCalled);

            inst.String();
            Assert.Equal("String", lastCalled);

            inst.StringWithParameters("StringWithParameters", 0, new TestClass());
            Assert.Equal("StringWithParameters", lastCalled);

            inst.TestClass();
            Assert.Equal("TestClass", lastCalled);

            inst.TestClassWithParameters("TestClassWithParameters", 0, new TestClass());
            Assert.Equal("TestClassWithParameters", lastCalled);
        }