コード例 #1
0
        public void TestPropertyAccessorBuilders()
        {
            var pID      = typeof(TestClass).GetProperty("ID", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);
            var pBool    = typeof(TestClass).GetProperty("BoolProp", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);
            var pIndexed = typeof(TestClass).GetProperty("Item", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);
            var pStatic  = typeof(TestClass).GetProperty("StaticText", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.FlattenHierarchy);

            var builder = new PropertyAccessorLambdaBuilder();

            var getterID = builder.BuildInstanceGetter <TestClass, int>(pID).Compile();
            var setterID = builder.BuildInstanceSetter <TestClass, int>(pID).Compile();
            //var getterID = builder.BuildGetterExpression(pID).Compile();
            //var setterID = builder.BuildSetterExpression(pID).Compile();

            var o = new TestClass(1)
            {
                BoolProp = true,
                I        = 9
            };

            var id = getterID.Invoke(o);

            Assert.True(id.Equals(1));

            setterID.Invoke(o, 2);
            Assert.True(o.GetID() == 2);

            var getterBool = builder.BuildInstanceGetter <TestClass, bool>(pBool).Compile();
            var setterBool = builder.BuildInstanceSetter <TestClass, bool>(pBool).Compile();

            var b = getterBool.Invoke(o);

            Assert.True(b);

            setterBool.Invoke(o, false);
            Assert.True(!o.BoolProp);

            var getterIndex = builder.BuildInstanceGetter <TestClass, long, object, object>(pIndexed).Compile();
            var setterIndex = builder.BuildInstanceSetter <TestClass, long, object, object>(pIndexed).Compile();

            var idx = getterIndex.Invoke(o, 10, "test");

            Assert.True((int)idx == 9);

            setterIndex.Invoke(o, 10, "test", 20);
            Assert.True(o.I == 10 && o.S == "test" && o.GetID() == 20);


            var getterStatic = builder.BuildStaticGetter <string>(pStatic).Compile();
            var setterStatic = builder.BuildStaticSetter <string>(pStatic).Compile();


            var t = getterStatic.Invoke();

            Assert.True(t == "test");

            setterStatic.Invoke("test2");
            t = getterStatic.Invoke();
            Assert.True(t == "test2");
        }
コード例 #2
0
        public Delegate BuildInstanceGetter(PropertyInfo propertyInfo, Type instanceType = null, Type valueType = null, IEnumerable <Type> indexerTypes = null)
        {
            CacheKey cacheKey = null;

            if (EnableCaching)
            {
                cacheKey = new CacheKey(propertyInfo, instanceType, valueType);
                if (getterCache.TryGetValue(cacheKey, out var cachedDelegate))
                {
                    return(cachedDelegate);
                }
            }

            var _delegate = builder.BuildInstanceGetter(propertyInfo, instanceType, valueType).Compile();

            if (EnableCaching)
            {
                getterCache.TryAdd(cacheKey, _delegate);
            }

            return(_delegate);
        }