Exemple #1
0
        public void SafelyNavigate_OnObjectWithLambdaThatGoesThroughFieldsAndReturnsProperty_ReturnsThePropertyValue()
        {
            var testObject = new ClassWithPublicFields
            {
                publicField = new ClassWithPublicFields(),
            };

            int expectedLength = testObject.publicField.StringRepresentation.Length;
            Assert.AreEqual(expectedLength, ObjectUtils.SafelyNavigate(testObject, x => x.publicField.StringRepresentation.Length));
        }
Exemple #2
0
        public void SafelyNavigate_OnObjectWithLambdaThatGoesThroughFieldsAndReturnsField_ReturnsTheFieldValue()
        {
            var returnedValue = new ClassWithPublicFields();
            var testObject = new ClassWithPublicFields
            {
                publicField = new ClassWithPublicFields
                {
                    publicField = returnedValue,
                }
            };

            Assert.AreSame(returnedValue, ObjectUtils.SafelyNavigate(testObject, x => x.publicField.publicField));
        }
        public void CreatePropertyBag_ClassWithPublicFields_PropertiesAreGenerated()
        {
            var propertyBag = new ReflectedPropertyBagProvider().CreatePropertyBag <ClassWithPublicFields>();

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

            Assert.That(propertyBag.HasProperty(ClassWithPublicFields.IntPropertyName), Is.True);
            Assert.That(propertyBag.HasProperty(ClassWithPublicFields.FloatPropertyName), Is.True);
            Assert.That(propertyBag.HasProperty(ClassWithPublicFields.MaskedPropertyName), Is.True);
            Assert.That(propertyBag.HasProperty(ClassWithPublicFields.SkippedPropertyName), Is.False);
            Assert.That(propertyBag.HasProperty(DerivedClassWithPublicFields.BoolPropertyName), Is.False);
            Assert.That(propertyBag.HasProperty(DerivedClassWithPublicFields.StringPropertyName), Is.False);

            var container = new ClassWithPublicFields();

            Assert.That(propertyBag.GetPropertyValue(ref container, ClassWithPublicFields.IntPropertyName), Is.EqualTo(42));
            Assert.That(propertyBag.GetPropertyValue(ref container, ClassWithPublicFields.FloatPropertyName), Is.EqualTo(123.456f));
            Assert.That(propertyBag.GetPropertyValue(ref container, ClassWithPublicFields.MaskedPropertyName), Is.EqualTo(1));
        }
        public void LoadAndStoreInstance()
        {
            FieldInfo fieldInfo     = typeof(ClassWithPublicFields).GetField("ReferenceTypeField");
            var       methodEmitter = GetMethodEmitter(false, typeof(string), new[] { typeof(ClassWithPublicFields) });

            LocalReference     local          = methodEmitter.DeclareLocal(typeof(string));
            FieldInfoReference fieldReference = new FieldInfoReference(methodEmitter.ArgumentReferences[0], fieldInfo);

            methodEmitter
            .AddStatement(new AssignStatement(local, fieldReference.ToExpression()))
            .AddStatement(new AssignStatement(fieldReference, new ConstReference("Replacement").ToExpression()))
            .AddStatement(new ReturnStatement(local));

            ClassWithPublicFields parameter = new ClassWithPublicFields();

            Assert.That(parameter.ReferenceTypeField, Is.EqualTo("Initial"));
            Assert.That(InvokeMethod(parameter), Is.EqualTo("Initial"));
            Assert.That(parameter.ReferenceTypeField, Is.EqualTo("Replacement"));
        }