예제 #1
0
        public void GetAttributeProxy_IsCached()
        {
            var provider = new StableBindingProxyProvider(
                new TypeLevelTypeFilter(new[] { typeof(ProxiedChild) }), CreateModuleScope("GetTypeMemberProxy"));

            var proxied0 = new ProxiedChildChildChild("ABCccccccccccccccccc");
            var proxied1 = new ProxiedChildChildChild("xyzzzzzzzzz");

            const string attributeName       = "PrependName";
            var          typeMemberProxy0    = provider.GetAttributeProxy(proxied0, attributeName);
            var          customMemberTester0 = new GetCustomMemberTester(typeMemberProxy0);
            var          result0             = ScriptingHelper.ExecuteScriptExpression <string> ("p0.XYZ('simsalbum',2)", customMemberTester0);

            Assert.That(result0, Is.EqualTo("ProxiedChild ProxiedChild: ABCccccccccccccccccc simsalbum, THE NUMBER=2"));


            var typeMemberProxy1 = provider.GetAttributeProxy(proxied1, attributeName);

            Assert.That(typeMemberProxy0, Is.SameAs(typeMemberProxy1));

            var customMemberTester1 = new GetCustomMemberTester(typeMemberProxy1);
            var result1             = ScriptingHelper.ExecuteScriptExpression <string> ("p0.ABCDEFG('Schlaf')", customMemberTester1);

            Assert.That(result1, Is.EqualTo("xyzzzzzzzzz Schlaf"));
        }
        public void BuildProxyType_PublicProperty_WithSetter()
        {
            var knownBaseTypes            = new[] { typeof(ProxiedChild) };
            var knownTypes                = knownBaseTypes; //knownBaseTypes.Union (knownInterfaceTypes).ToArray ();
            var typeFilter                = new TypeLevelTypeFilter(knownTypes);
            var proxiedType               = typeof(ProxiedChildChildChild);
            var stableBindingProxyBuilder = new StableBindingProxyBuilder(proxiedType, typeFilter, CreateModuleScope("BuildProxyType_PublicProperty"));
            var proxyType = stableBindingProxyBuilder.BuildProxyType();

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new ProxiedChildChildChild("PC");
            object proxy   = Activator.CreateInstance(proxyType, proxied);

            Assert.That(proxied.NameProperty, Is.EqualTo("ProxiedChildChildChild::NameProperty PC"));

            //To.ConsoleLine.e ("proxyType.GetAllProperties()", proxyType.GetAllProperties ()).nl ().e (proxyType.GetAllProperties ().Select (pi => pi.Attributes)).nl (2).e ("proxyType.GetAllMethods()", proxyType.GetAllMethods ());

            var proxyPropertyInfo = proxyType.GetProperty("NameProperty", _publicInstanceFlags);

            Assert.That(proxyPropertyInfo, Is.Not.Null);
            Assert.That(proxyPropertyInfo.CanRead, Is.True);
            Assert.That(proxyPropertyInfo.CanWrite, Is.True);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChild::NameProperty PC"));

            proxyPropertyInfo.SetValue(proxy, "XXyyZZ", null);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChild::NameProperty XXyyZZ-ProxiedChild::NameProperty"));
        }
        public void BuildProxyType_VirtualPublicProperty_WithSetter()
        {
            var knownBaseTypes = new[] { typeof(ProxiedChild) };
            //var knownInterfaceTypes = new[] { typeof (IProperty) };
            var knownTypes  = knownBaseTypes; //knownBaseTypes.Union (knownInterfaceTypes).ToArray ();
            var typeFilter  = new TypeLevelTypeFilter(knownTypes);
            var proxiedType = typeof(ProxiedChildChildChild);
            var stableBindingProxyBuilder = new StableBindingProxyBuilder(proxiedType, typeFilter, CreateModuleScope("BuildProxyType_PublicProperty"));
            var proxyType = stableBindingProxyBuilder.BuildProxyType();

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new ProxiedChildChildChild("PC");
            object proxy   = Activator.CreateInstance(proxyType, proxied);

            Assert.That(proxied.MutableNamePropertyVirtual, Is.EqualTo("ProxiedChildChild::MutableNamePropertyVirtual PC"));

            //To.ConsoleLine.e ("proxyType.GetAllProperties()", proxyType.GetAllProperties ()).nl ().e (proxyType.GetAllProperties ().Select (pi => pi.Attributes)).nl (2).e ("proxyType.GetAllMethods()", proxyType.GetAllMethods ());

            var proxyPropertyInfo = proxyType.GetProperty("MutableNamePropertyVirtual", _publicInstanceFlags);


            // Note: Virtual properties remain virtual, so the proxy calls go to ProxiedChildChild, not the first known base type of ProxiedChild
            // (ProxiedChildChildChild does not override MutableNamePropertyVirtual).

            Assert.That(proxyPropertyInfo, Is.Not.Null);
            Assert.That(proxyPropertyInfo.CanRead, Is.True);
            Assert.That(proxyPropertyInfo.CanWrite, Is.True);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChildChild::MutableNamePropertyVirtual PC"));

            proxyPropertyInfo.SetValue(proxy, "XXyyZZ", null);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChildChild::MutableNamePropertyVirtual XXyyZZ-ProxiedChildChild::MutableNamePropertyVirtual"));
        }
        public void BuildProxyType_AmbigousExplicitInterfaceProperties_With_PublicInterfaceImplementation()
        {
            var knownBaseTypes            = new[] { typeof(ProxiedChildChild) };
            var knownInterfaceTypes       = new[] { typeof(IProperty), typeof(IPropertyAmbigous2) };
            var knownTypes                = knownBaseTypes.Union(knownInterfaceTypes).ToArray();
            var typeFilter                = new TypeLevelTypeFilter(knownTypes);
            var proxiedType               = typeof(ProxiedChildChildChild);
            var stableBindingProxyBuilder = new StableBindingProxyBuilder(proxiedType, typeFilter, CreateModuleScope("BuildProxyType_ExplicitInterfaceProperty"));
            var proxyType = stableBindingProxyBuilder.BuildProxyType();

            Assert.That(proxyType.GetInterface("IPropertyAmbigous2"), Is.Not.Null);
            Assert.That(proxyType.GetInterface("IPropertyAmbigous1"), Is.Null);

            // Create proxy instance, initializing it with class to be proxied
            var proxied = new ProxiedChildChildChild("PC");

            object proxy = Activator.CreateInstance(proxyType, proxied);

            const string expectedPropertyValue = "ProxiedChildChild::IPropertyAmbigous2::PropertyAmbigous PC";

            Assert.That(((IPropertyAmbigous2)proxied).PropertyAmbigous, Is.EqualTo(expectedPropertyValue));

            //To.ConsoleLine.e ("proxyType.GetAllProperties()", proxyType.GetAllProperties ()).nl ().e (proxyType.GetAllProperties ().Select(pi => pi.Attributes)).nl (2).e ("proxyType.GetAllMethods()", proxyType.GetAllMethods ());


            var proxyPropertyInfoPublicProperty = proxyType.GetProperty("PropertyAmbigous", _publicInstanceFlags);

            Assert.That(proxyPropertyInfoPublicProperty, Is.Not.Null);

            Assert.That(ScriptingHelper.ExecuteScriptExpression <string> ("p0.PropertyAmbigous", proxy), Is.EqualTo("ProxiedChildChild::PropertyAmbigous PC"));

            var proxyPropertyInfo = proxyType.GetProperty(
                "Remotion.Scripting.UnitTests.TestDomain.ProxiedChild.Remotion.Scripting.UnitTests.TestDomain.IPropertyAmbigous2.PropertyAmbigous", _nonPublicInstanceFlags);

            Assert.That(proxyPropertyInfo, Is.Not.Null);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo(expectedPropertyValue));
            //AssertPropertyInfoEqual (proxyPropertyInfo, propertyInfo);

            ((IPropertyAmbigous2)proxied).PropertyAmbigous = "aBc";
            const string expectedPropertyValue2 = "ProxiedChildChild::IPropertyAmbigous2::PropertyAmbigous aBc-ProxiedChildChild::IPropertyAmbigous2::PropertyAmbigous";

            Assert.That(((IPropertyAmbigous2)proxied).PropertyAmbigous, Is.EqualTo(expectedPropertyValue2));
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo(expectedPropertyValue2));

            proxyPropertyInfo.SetValue(proxy, "XXyyZZ", null);
            const string expectedPropertyValue3 = "ProxiedChildChild::IPropertyAmbigous2::PropertyAmbigous XXyyZZ-ProxiedChildChild::IPropertyAmbigous2::PropertyAmbigous";

            Assert.That(((IPropertyAmbigous2)proxied).PropertyAmbigous, Is.EqualTo(expectedPropertyValue3));
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo(expectedPropertyValue3));

            var proxyPropertyInfo2 = proxyType.GetProperty(
                "Remotion.Scripting.UnitTests.TestDomain.ProxiedChild.Remotion.Scripting.UnitTests.TestDomain.IPropertyAmbigous1.PropertyAmbigous", _nonPublicInstanceFlags);

            Assert.That(proxyPropertyInfo2, Is.Null);
        }
예제 #5
0
        public void GetTypeMemberProxy()
        {
            var provider = new StableBindingProxyProvider(
                new TypeLevelTypeFilter(new[] { typeof(ProxiedChild) }), CreateModuleScope("GetTypeMemberProxy"));

            var          proxied       = new ProxiedChildChildChild("abrakadava");
            const string attributeName = "PrependName";

            var typeMemberProxy = provider.GetAttributeProxy(proxied, attributeName);

            var customMemberTester = new GetCustomMemberTester(typeMemberProxy);

            var result = ScriptingHelper.ExecuteScriptExpression <string> ("p0.XYZ('simsalbum',2)", customMemberTester);

            Assert.That(result, Is.EqualTo("ProxiedChild ProxiedChild: abrakadava simsalbum, THE NUMBER=2"));
        }
예제 #6
0
        public void BuildProxyType()
        {
            var typeFilter = new TypeLevelTypeFilter(new[] { typeof(ProxiedChild) });
            var provider   = new StableBindingProxyProvider(
                typeFilter, CreateModuleScope("GetTypeMemberProxy"));

            Assert.That(provider.TypeFilter, Is.SameAs(typeFilter));

            var          proxied       = new ProxiedChildChildChild("abrakadava");
            const string attributeName = "PrependName";

            var proxyType = provider.BuildProxyType(proxied.GetType());

            var proxyMethod = proxyType.GetAllInstanceMethods(attributeName, typeof(string)).Single();

            Assert.That(proxyMethod, Is.Not.Null);
        }
예제 #7
0
        public void BuildProxy()
        {
            var provider = new StableBindingProxyProvider(
                new TypeLevelTypeFilter(new[] { typeof(ProxiedChild) }), CreateModuleScope("BuildProxy"));

            var proxied = new ProxiedChildChildChild("abrakadava");

            var proxy = provider.BuildProxy(proxied);

            // Necessary since a newly built proxy has an empty proxied field
            // TODO: Introduce BuildProxyFromType(proxiedType)
            ScriptingHelper.SetProxiedFieldValue(proxy, proxied);

            Assert.That(proxy, Is.Not.Null);

            var result = ScriptingHelper.ExecuteScriptExpression <string> ("p0.PrependName('simsalbum',2)", proxy);

            Assert.That(result, Is.EqualTo("ProxiedChild ProxiedChild: abrakadava simsalbum, THE NUMBER=2"));
        }
        public void BuildProxyType_PublicProperty()
        {
            var knownBaseTypes            = new[] { typeof(ProxiedChildChild) };
            var knownTypes                = knownBaseTypes; //knownBaseTypes.Union (knownInterfaceTypes).ToArray ();
            var typeFilter                = new TypeLevelTypeFilter(knownTypes);
            var proxiedType               = typeof(ProxiedChildChildChild);
            var stableBindingProxyBuilder = new StableBindingProxyBuilder(proxiedType, typeFilter, CreateModuleScope("BuildProxyType_PublicProperty"));
            var proxyType = stableBindingProxyBuilder.BuildProxyType();

            // Create proxy instance, initializing it with class to be proxied
            var    proxied = new ProxiedChildChildChild("PC");
            object proxy   = Activator.CreateInstance(proxyType, proxied);

            Assert.That(proxied.NameProperty, Is.EqualTo("ProxiedChildChildChild::NameProperty PC"));

            var proxyPropertyInfo = proxyType.GetProperty("NameProperty", _publicInstanceFlags);

            Assert.That(proxyPropertyInfo, Is.Not.Null);
            Assert.That(proxyPropertyInfo.CanRead, Is.True);
            Assert.That(proxyPropertyInfo.CanWrite, Is.False);
            Assert.That(proxyPropertyInfo.GetValue(proxy, null), Is.EqualTo("ProxiedChildChild::NameProperty PC"));
        }