Example #1
0
            public void BuildAccessorForDifferentEntity_Exception()
            {
                var field = GetField <ClassWithPublicField>();

                Assert.Throws <ArgumentException>(
                    () => FieldAccessorBuilder.BuildFor(typeof(ClassWithPrivateReadonlyField), field, true));
            }
Example #2
0
        public void SetUp()
        {
            someClass = new SomeClass();
            var field = typeof(SomeClass).GetField("field", BindingFlags.NonPublic | BindingFlags.Instance);

            var xValue          = Expression.Parameter(typeof(object));
            var xContainer      = Expression.Parameter(typeof(object));
            var xTypedContainer = Expression.Parameter(typeof(SomeClass));
            var xSetField       = Expression
                                  .Lambda <Func <object, object, object> >(
                Expression.Block(new[] { xTypedContainer },
                                 Expression.Assign(
                                     xTypedContainer,
                                     Expression.Convert(xContainer, typeof(SomeClass))),
                                 Expression.Assign(
                                     Expression.Field(xTypedContainer, field),
                                     Expression.Convert(xValue, typeof(int))),
                                 Expression.Convert(xTypedContainer, typeof(object))),
                xContainer, xValue);

            setViaExpression = xSetField.Compile();

            var memberAccessor = FieldAccessorBuilder.BuildFor(typeof(SomeClass), field, true);

            setViaEmit = memberAccessor.SetMember;

            setViaReflection = (container, value) =>
            {
                field.SetValue(container, value);
                return(container);
            };
        }
Example #3
0
            public void BuildAccessorForInheritedEntity_NoException()
            {
                var field = GetField <ClassWithPrivateReadonlyField>();

                Assert.DoesNotThrow(
                    () => FieldAccessorBuilder.BuildFor(typeof(ClassWithInheritedPrivateReadonlyField), field, true));
            }
Example #4
0
            public MemberReplicationInfo(Type containerType, FieldReplicationInfo fieldReplicationInfo)
            {
                Behavior = fieldReplicationInfo.Behavior;
                IMemberAccessor memberAccessor = FieldAccessorBuilder.BuildFor(containerType, fieldReplicationInfo.Field, true);

                GetMember = memberAccessor.GetMember;
                SetMember = memberAccessor.SetMember;
            }
Example #5
0
            public void BuildAccessorForInheritedInternalField_CantGetAndSet()
            {
                var field = GetField <ClassWithInheritedInternalField>();

                var result = FieldAccessorBuilder.BuildFor(typeof(ClassWithInheritedInternalField), field);

                Assert.That(result.CanGet, Is.False);
                Assert.That(result.CanSet, Is.False);
            }
Example #6
0
            public void BuildAccessorForPublicReadonlyField_CantSet()
            {
                var field = GetField <ClassWithPublicReadonlyField>();

                var result = FieldAccessorBuilder.BuildFor(typeof(ClassWithPublicReadonlyField), field);

                Assert.That(result.CanGet, Is.True);
                Assert.That(result.CanSet, Is.False);
            }
Example #7
0
            public void BuildAccessorForInternalFieldOfBase_ItCanReadAndWrite()
            {
                var field = GetField <ClassWithInheritedInternalReadonlyField>();

                var accessor = FieldAccessorBuilder.BuildFor(typeof(ClassWithInheritedInternalReadonlyField), field, true);

                var data = new ClassWithInheritedInternalReadonlyField(RandomInt());

                Assert.That(accessor.GetMember(data), Is.EqualTo(data.GetField()));
                var fieldValue = RandomInt();

                accessor.SetMember(data, fieldValue);
                Assert.That(data.GetField(), Is.EqualTo(fieldValue));
            }
Example #8
0
            public void BuildAccessorForStructureField_ItCanReadAndWrite()
            {
                var field = GetField <Structure>();

                var accessor = FieldAccessorBuilder.BuildFor(typeof(Structure), field, true);

                var source = new Structure {
                    field = RandomInt()
                };

                Assert.That(accessor.GetMember(source), Is.EqualTo(source.field));
                var fieldValue = RandomInt();
                var result     = (Structure)accessor.SetMember(source, fieldValue);

                Assert.That(result.field, Is.EqualTo(fieldValue));
            }
Example #9
0
            public void BuildAccessorForPublicField_ItCanReadAndWrite()
            {
                var field = GetField <ClassWithPublicField>();

                var accessor = FieldAccessorBuilder.BuildFor(typeof(ClassWithPublicField), field, true);

                Assert.That(accessor.CanGet && accessor.CanSet);
                var data = new ClassWithPublicField {
                    field = RandomInt()
                };

                Assert.That(accessor.GetMember(data), Is.EqualTo(data.field));
                var fieldValue = RandomInt();

                accessor.SetMember(data, fieldValue);
                Assert.That(data.field, Is.EqualTo(fieldValue));
            }