コード例 #1
0
        public void PropertyGetterCanCastValue()
        {
            var type4       = typeof(Class4);
            var propClassA4 = type4.GetProperty("ClassAValue");
            var propInt4    = type4.GetProperty("IntValue");

            var object2A = new Class2A();
            var object4  = new Class4 {
                ClassAValue = object2A, IntValue = 159
            };

            // can cast the return type from Class2A to Class2
            var getterClassA4 = ReflectionUtilities.EmitPropertyGetter <Class4, Class2>(propClassA4);

            var valueClass4A = getterClassA4(object4);

            Assert.IsNotNull(valueClass4A);
            Assert.AreSame(object2A, valueClass4A);

            // cannot cast the return type from Class2A to Class3!
            Assert.Throws <ArgumentException>(()
                                              => ReflectionUtilities.EmitPropertyGetter <Class4, Class3>(propClassA4));

            // can cast and box the return type from int to object
            var getterInt4 = ReflectionUtilities.EmitPropertyGetter <Class4, object>(propInt4);

            var valueInt4 = getterInt4(object4);

            Assert.IsTrue(valueInt4 is int);
            Assert.AreEqual(159, valueInt4);

            // cannot cast the return type from int to Class3!
            Assert.Throws <ArgumentException>(()
                                              => ReflectionUtilities.EmitPropertyGetter <Class4, Class3>(propInt4));
        }
コード例 #2
0
ファイル: Basic.cs プロジェクト: TimCollins/DesignPatterns
        public void PerformAction()
        {
            Class1A c1a = new Class1A();
            Class1B c1b = new Class1B();
            Class2A c2a = new Class2A();
            Class2B c2b = new Class2B();

            int result1a = c1a.Method1A();
            int result1b = c1b.Method1B(result1a);
            int result2a = c2a.Method2A(result1a);
            c2b.Method2B(result1b, result2a);
        }
コード例 #3
0
        public void PropertySetterCanCastUnsafeValue()
        {
            // test that we can emit property setters that cast from eg 'object'

            var type4       = typeof(Class4);
            var propInt4    = type4.GetProperty("IntValue");
            var propString4 = type4.GetProperty("StringValue");
            var propClassA4 = type4.GetProperty("ClassAValue");

            var object4  = new Class4();
            var object2A = new Class2A();

            // works with a string property
            Assert.IsNotNull(propString4);
            var setterString4 = ReflectionUtilities.EmitPropertySetterUnsafe <Class4, object>(propString4);

            Assert.IsNotNull(setterString4);
            setterString4(object4, "foo");
            Assert.IsNotNull(object4.StringValue);
            Assert.AreEqual("foo", object4.StringValue);

            // unsafe is... unsafe
            Assert.Throws <InvalidCastException>(() => setterString4(object4, new Class2()));

            // works with a reference property
            Assert.IsNotNull(propClassA4);
            var setterClassA4 = ReflectionUtilities.EmitPropertySetterUnsafe <Class4, object>(propClassA4);

            Assert.IsNotNull(setterClassA4);
            setterClassA4(object4, object2A);
            Assert.IsNotNull(object4.ClassAValue);
            Assert.AreEqual(object2A, object4.ClassAValue);

            // works with a boxed value type
            Assert.IsNotNull(propInt4);
            var setterInt4 = ReflectionUtilities.EmitPropertySetterUnsafe <Class4, object>(propInt4);

            Assert.IsNotNull(setterInt4);

            setterInt4(object4, 42);
            Assert.AreEqual(42, object4.IntValue);

            // fixme the code below runs fine with ReSharper test running within VisualStudio
            // but it crashes when running via vstest.console.exe - unless some settings are required?

            // converting works
            setterInt4(object4, 42.0);
            Assert.AreEqual(42, object4.IntValue);

            // unsafe is... unsafe
            Assert.Throws <FormatException>(() => setterInt4(object4, "foo"));
        }
コード例 #4
0
        public void PerformAction()
        {
            Class1A c1a = new Class1A();
            Class1B c1b = new Class1B();
            Class2A c2a = new Class2A();
            Class2B c2b = new Class2B();

            int result1a = c1a.Method1A();
            int result1b = c1b.Method1B(result1a);
            int result2a = c2a.Method2A(result1a);

            c2b.Method2B(result1b, result2a);
        }
コード例 #5
0
 public void SaveWithChildren(Class2A item)
 {
     this.Save(item.SubClassA);
     this.sqliteConnection.InsertOrReplaceAllWithChildren(item.ListClass2B);
     this.sqliteConnection.InsertOrReplaceWithChildren(item, true);
 }