コード例 #1
0
            public bool CanRead(IEvaluationContext context, object target, string name)
            {
                if (target == null)
                {
                    return(false);
                }

                var type = target is Type ? (Type)target : target.GetType();

                if (type.IsArray)
                {
                    return(false);
                }

                if (_member is MethodInfo)
                {
                    var method     = (MethodInfo)_member;
                    var getterName = "get_" + ReflectivePropertyAccessor.Capitalize(name);
                    if (getterName.Equals(method.Name))
                    {
                        return(true);
                    }

                    return(false);
                }
                else
                {
                    var field = (FieldInfo)_member;
                    return(field.Name.Equals(name));
                }
            }
コード例 #2
0
        public void TestOptimalReflectivePropertyAccessor()
        {
            var reflective = new ReflectivePropertyAccessor();
            var tester     = new Tester
            {
                Property = "hello"
            };
            var ctx = new StandardEvaluationContext(tester);

            Assert.True(reflective.CanRead(ctx, tester, "Property"));
            Assert.Equal("hello", reflective.Read(ctx, tester, "Property").Value);

            // cached accessor used
            Assert.Equal("hello", reflective.Read(ctx, tester, "Property").Value);

            var property = reflective.CreateOptimalAccessor(ctx, tester, "Property");

            Assert.True(property.CanRead(ctx, tester, "Property"));
            Assert.False(property.CanRead(ctx, tester, "Property2"));
            Assert.Throws <InvalidOperationException>(() => property.CanWrite(ctx, tester, "Property"));
            Assert.Throws <InvalidOperationException>(() => property.CanWrite(ctx, tester, "Property2"));
            Assert.Equal("hello", property.Read(ctx, tester, "Property").Value);

            // cached accessor used
            Assert.Equal("hello", property.Read(ctx, tester, "Property").Value);
            Assert.Throws <InvalidOperationException>(() => property.GetSpecificTargetClasses());
            Assert.Throws <InvalidOperationException>(() => property.Write(ctx, tester, "Property", null));

            var field = reflective.CreateOptimalAccessor(ctx, tester, "Field");

            Assert.True(field.CanRead(ctx, tester, "Field"));
            Assert.False(field.CanRead(ctx, tester, "Field2"));
            Assert.Throws <InvalidOperationException>(() => field.CanWrite(ctx, tester, "Field"));
            Assert.Throws <InvalidOperationException>(() => field.CanWrite(ctx, tester, "Field2"));
            Assert.Equal(3, field.Read(ctx, tester, "Field").Value);

            // cached accessor used
            Assert.Equal(3, field.Read(ctx, tester, "Field").Value);
            Assert.Throws <InvalidOperationException>(() => field.GetSpecificTargetClasses());
            Assert.Throws <InvalidOperationException>(() => field.Write(ctx, tester, "field", null));
        }
コード例 #3
0
        public void TestReflectivePropertyAccessor()
        {
            var rpa = new ReflectivePropertyAccessor();
            var t   = new Tester
            {
                Property = "hello"
            };
            var ctx = new StandardEvaluationContext(t);

            Assert.True(rpa.CanRead(ctx, t, "Property"));
            Assert.Equal("hello", rpa.Read(ctx, t, "Property").Value);

            // cached accessor used
            Assert.Equal("hello", rpa.Read(ctx, t, "Property").Value);

            Assert.True(rpa.CanRead(ctx, t, "Field"));
            Assert.Equal(3, rpa.Read(ctx, t, "Field").Value);

            // cached accessor used
            Assert.Equal(3, rpa.Read(ctx, t, "Field").Value);

            Assert.True(rpa.CanWrite(ctx, t, "Property"));
            rpa.Write(ctx, t, "Property", "goodbye");
            rpa.Write(ctx, t, "Property", "goodbye"); // cached accessor used

            Assert.True(rpa.CanWrite(ctx, t, "Field"));
            rpa.Write(ctx, t, "Field", 12);
            rpa.Write(ctx, t, "Field", 12);

            // Attempted Write as first activity on this field and property to drive testing
            // of populating type descriptor cache
            rpa.Write(ctx, t, "Field2", 3);
            rpa.Write(ctx, t, "Property2", "doodoo");
            Assert.Equal(3, rpa.Read(ctx, t, "Field2").Value);

            // Attempted Read as first activity on this field and property (no CanRead before them)
            Assert.Equal(0, rpa.Read(ctx, t, "Field3").Value);
            Assert.Equal("doodoo", rpa.Read(ctx, t, "Property3").Value);

            // Access through is method
            Assert.Equal(0, rpa.Read(ctx, t, "Field3").Value);
            Assert.False((bool)rpa.Read(ctx, t, "Property4").Value);
            Assert.True(rpa.CanRead(ctx, t, "Property4"));

            // repro SPR-9123, ReflectivePropertyAccessor JavaBean property names compliance tests
            Assert.Equal("iD", rpa.Read(ctx, t, "iD").Value);
            Assert.True(rpa.CanRead(ctx, t, "iD"));
            Assert.Equal("id", rpa.Read(ctx, t, "Id").Value);
            Assert.True(rpa.CanRead(ctx, t, "Id"));
            Assert.Equal("ID", rpa.Read(ctx, t, "ID").Value);
            Assert.True(rpa.CanRead(ctx, t, "ID"));

            // note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
            Assert.Equal("id", rpa.Read(ctx, t, "Id").Value);
            Assert.True(rpa.CanRead(ctx, t, "Id"));

            // repro SPR-10994
            Assert.Equal("xyZ", rpa.Read(ctx, t, "XyZ").Value);
            Assert.True(rpa.CanRead(ctx, t, "XyZ"));
            Assert.Equal("xY", rpa.Read(ctx, t, "XY").Value);
            Assert.True(rpa.CanRead(ctx, t, "XY"));

            // SPR-10122, ReflectivePropertyAccessor JavaBean property names compliance tests - setters
            rpa.Write(ctx, t, "pEBS", "Test String");
            Assert.Equal("Test String", rpa.Read(ctx, t, "pEBS").Value);
        }