예제 #1
0
        private void MakeDelegates()
        {
            var instance = Expression.Parameter(InstanceType.MakeByRefType(), "instance");
            var value    = Expression.Parameter(ReturnType, "value");

            Expression instance_field = Expression.MakeMemberAccess(instance, Field);

            Expression getExpression = null;
            Expression setExpression = null;

            if (ReturnType == Field.FieldType)
            {
                setExpression = Expression.Assign(instance_field, value);
                getExpression = instance_field;
            }
            else
            {
                if (Field.FieldType.IsValueType)
                {
                    setExpression = Expression.Assign(instance_field, Expression.Convert(value, Field.FieldType));
                    getExpression = Expression.Convert(instance_field, ReturnType);
                }
                else
                {
                    setExpression = Expression.Assign(getExpression, Expression.TypeAs(value, Field.FieldType));
                    getExpression = Expression.TypeAs(instance_field, ReturnType);
                }
            }

            Get = Expression.Lambda <FuncRef <T, P> >(getExpression, instance).Compile();
            Set = Expression.Lambda <ActionRef <T, P> >(setExpression, instance, value).Compile();
        }