Ejemplo n.º 1
0
        public void CreateSetterForClass()
        {
            var intField = typeof(InternalClass).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var intSetter = FieldAccessorGenerator.CreateSetter(intField);
            var c         = new InternalClass2();

            Assert.Throws <InvalidCastException>(() => intSetter(c, "123"));
            intSetter(c, 1234);
            Assert.AreEqual(1234, intField.GetValue(c));

            var stringField = typeof(InternalClass).GetField("_stringField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(stringField);

            var stringSetter = FieldAccessorGenerator.CreateSetter(stringField);

            Assert.Throws <InvalidCastException>(() => stringSetter(c, 123));
            stringSetter(c, "string");
            Assert.AreEqual("string", stringField.GetValue(c));

            stringSetter(c, null);
            Assert.AreEqual(null, stringField.GetValue(c));
        }
Ejemplo n.º 2
0
        public void CreateGenericSetterForStruct()
        {
            var intField = typeof(InternalStruct).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var    intSetter = FieldAccessorGenerator.CreateSetter <object, int>(intField);
            object s         = new InternalStruct();

            intSetter(s, 876);
            Assert.AreEqual(876, intField.GetValue(s));

            var intSetter2 = FieldAccessorGenerator.CreateSetter <object, object>(intField);

            intSetter2(s, 8762);
            Assert.AreEqual(8762, intField.GetValue(s));

            var stringField = typeof(InternalStruct).GetField("_stringField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(stringField);

            var stringSetter = FieldAccessorGenerator.CreateSetter <object, string>(stringField);

            stringSetter(s, "str1");
            Assert.AreEqual("str1", stringField.GetValue(s));

            stringSetter(s, null);
            Assert.AreEqual(null, stringField.GetValue(s));

            var stringSetter2 = FieldAccessorGenerator.CreateSetter <object, object>(stringField);

            stringSetter2(s, "str12");
            Assert.AreEqual("str12", stringField.GetValue(s));
        }
Ejemplo n.º 3
0
        public void CreateGenericSetterForClass()
        {
            var intField = typeof(InternalClass).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var intSetter = FieldAccessorGenerator.CreateSetter <InternalClass2, int>(intField);
            var c         = new InternalClass2();

            intSetter(c, 1234);
            Assert.AreEqual(1234, intField.GetValue(c));

            var intSetter2 = FieldAccessorGenerator.CreateSetter <InternalClass2, object>(intField);

            intSetter2(c, 12342);
            Assert.AreEqual(12342, intField.GetValue(c));

            var intSetter3 = FieldAccessorGenerator.CreateSetter <object, int>(intField);

            intSetter3(c, 123423);
            Assert.AreEqual(123423, intField.GetValue(c));

            var stringField = typeof(InternalClass).GetField("_stringField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(stringField);

            var stringSetter = FieldAccessorGenerator.CreateSetter <InternalClass, string>(stringField);

            stringSetter(c, "string");
            Assert.AreEqual("string", stringField.GetValue(c));

            stringSetter(c, null);
            Assert.AreEqual(null, stringField.GetValue(c));
        }
Ejemplo n.º 4
0
        public void CreateSetterForStruct()
        {
            var intField = typeof(InternalStruct).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var    intSetter = FieldAccessorGenerator.CreateSetter(intField);
            object s         = new InternalStruct();

            Assert.Throws <InvalidCastException>(() => intSetter(s, "123"));
            intSetter(s, 876);
            Assert.AreEqual(876, intField.GetValue(s));

            var stringField = typeof(InternalStruct).GetField("_stringField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(stringField);

            var stringSetter = FieldAccessorGenerator.CreateSetter(stringField);

            Assert.Throws <InvalidCastException>(() => stringSetter(s, 123));
            stringSetter(s, "str1");
            Assert.AreEqual("str1", stringField.GetValue(s));

            stringSetter(s, null);
            Assert.AreEqual(null, stringField.GetValue(s));
        }
Ejemplo n.º 5
0
        public void CreateGenericSetterThrowsException()
        {
            var fieldFromClass = typeof(InternalClass).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(fieldFromClass);

            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <IInternalInterface, int>(fieldFromClass));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <IInternalInterface, string>(fieldFromClass));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <InternalStruct, int>(fieldFromClass));

            var fieldFromStruct = typeof(InternalStruct).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(fieldFromStruct);

            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <IInternalInterface, int>(fieldFromStruct));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <InternalClass, int>(fieldFromStruct));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateSetter <int, int>(fieldFromStruct));
        }