Exemplo n.º 1
0
        public void CreateGenericGetterThrowsException()
        {
            var fieldFromClass = typeof(InternalClass).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

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

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

            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateGetter <IInternalInterface, int>(fieldFromStruct));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateGetter <InternalClass, int>(fieldFromStruct));
            Assert.Throws <ArgumentException>(() => FieldAccessorGenerator.CreateGetter <int, int>(fieldFromStruct));
        }
Exemplo n.º 2
0
        public void CreateGetterForStaticFields()
        {
            var fieldFromClass = typeof(InternalClass).GetField("StaticField", BindingFlags.Static | BindingFlags.Public);

            Assert.NotNull(fieldFromClass);
            var getterForClass = FieldAccessorGenerator.CreateGetter(fieldFromClass);

            InternalClass.StaticField = 1234;
            Assert.AreEqual(InternalClass.StaticField, getterForClass(null));

            var fieldFromStruct = typeof(InternalStruct).GetField("StaticField", BindingFlags.Static | BindingFlags.Public);

            Assert.NotNull(fieldFromStruct);
            var getterForStruct = FieldAccessorGenerator.CreateGetter(fieldFromStruct);

            InternalStruct.StaticField = 4321;
            Assert.AreEqual(InternalStruct.StaticField, getterForStruct(null));
        }
Exemplo n.º 3
0
        public void CreateGenericGetterForStruct()
        {
            var intField = typeof(InternalStruct).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var intGetter = FieldAccessorGenerator.CreateGetter <InternalStruct, int>(intField);
            var s         = new InternalStruct();

            intField.SetValue(s, 1231);
            Assert.AreEqual(intField.GetValue(s), intGetter(s));

            var intGetter2 = FieldAccessorGenerator.CreateGetter <object, int>(intField);

            intField.SetValue(s, 1232);
            Assert.AreEqual(intField.GetValue(s), intGetter2(s));

            var intGetter3 = FieldAccessorGenerator.CreateGetter <InternalStruct, object>(intField);

            intField.SetValue(s, 1233);
            Assert.AreEqual(intField.GetValue(s), intGetter3(s));

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

            Assert.NotNull(stringField);

            var stringGetter = FieldAccessorGenerator.CreateGetter <InternalStruct, string>(stringField);

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

            stringField.SetValue(s, null);
            Assert.AreEqual(null, stringGetter(s));

            var stringGetter2 = FieldAccessorGenerator.CreateGetter <InternalStruct, object>(stringField);

            stringField.SetValue(s, "str2");
            Assert.AreEqual(stringField.GetValue(s), stringGetter2(s));

            var stringGetter3 = FieldAccessorGenerator.CreateGetter <object, string>(stringField);

            stringField.SetValue(s, "str3");
            Assert.AreEqual(stringField.GetValue(s), stringGetter3(s));
        }
Exemplo n.º 4
0
        public void CreateGenericGetterForClass()
        {
            var intField = typeof(InternalClass).GetField("_intField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(intField);

            var intGetter = FieldAccessorGenerator.CreateGetter <InternalClass2, int>(intField);
            var c         = new InternalClass2();

            intField.SetValue(c, 123);
            Assert.AreEqual(intField.GetValue(c), intGetter(c));

            var intGetter2 = FieldAccessorGenerator.CreateGetter <InternalClass, object>(intField);

            intField.SetValue(c, 1232);
            Assert.AreEqual(intField.GetValue(c), intGetter2(c));

            var intGetter3 = FieldAccessorGenerator.CreateGetter <object, int>(intField);

            intField.SetValue(c, 1233);
            Assert.AreEqual(intField.GetValue(c), intGetter3(c));

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

            Assert.NotNull(stringField);

            var stringGetter = FieldAccessorGenerator.CreateGetter <InternalClass, string>(stringField);

            stringField.SetValue(c, "str");
            Assert.AreEqual(stringField.GetValue(c), stringGetter(c));

            stringField.SetValue(c, null);
            Assert.AreEqual(null, stringGetter(c));

            var stringGetter2 = FieldAccessorGenerator.CreateGetter <InternalClass2, object>(stringField);

            stringField.SetValue(c, "str2");
            Assert.AreEqual(stringField.GetValue(c), stringGetter2(c));

            var stringGetter3 = FieldAccessorGenerator.CreateGetter <object, string>(stringField);

            stringField.SetValue(c, "str3");
            Assert.AreEqual(stringField.GetValue(c), stringGetter3(c));
        }