Exemplo n.º 1
0
        public Expression <Func <T2, T1> > CreateFromMethod()
        {
            var value2 = Expression.Parameter(Type2);
            var value1 = Expression.Variable(Type1);

            List <Expression> list = new List <Expression>();

            if (TransformerHelper.IsEqualsTypes(Type1, Type2))
            {
                list.Add(Expression.Label(Expression.Label(Type2), value2));
            }
            else
            {
                list.Add(TransformerHelper.BuildBody(value1, value2, MembersOrder1, MembersOrder2));
                list.Add(Expression.Label(Expression.Label(Type1), value1));
            }

            return(Expression.Lambda <Func <T2, T1> >(TransformerHelper.IsEqualsTypes(Type1, Type2) ? list[0] : Expression.Block(typeof(T1), new ParameterExpression[] { value1 }, list), value2));
        }
Exemplo n.º 2
0
        public Expression <Func <T, IData> > CreateToMethod()
        {
            var value = Expression.Parameter(Type1);
            var data  = Expression.Variable(typeof(Data <>).MakeGenericType(Type2));

            List <Expression> list = new List <Expression>();

            if (TransformerHelper.IsEqualsTypes(Type1, Type2))
            {
                list.Add(Expression.Label(Expression.Label(typeof(IData)), Expression.New(data.Type.GetConstructor(new Type[] { Type1 }), value)));
            }
            else
            {
                list.Add(Expression.Assign(data, Expression.New(data.Type.GetConstructor(new Type[] { }))));
                list.Add(TransformerHelper.BuildBody(data.Value(), value, MembersOrder1, MembersOrder2));
                list.Add(Expression.Label(Expression.Label(data.Type), data));
            }

            return(Expression.Lambda <Func <T, IData> >(TransformerHelper.IsEqualsTypes(Type1, Type2) ? list[0] : Expression.Block(typeof(IData), new ParameterExpression[] { data }, list), value));
        }
Exemplo n.º 3
0
        public Expression <Func <IData, T> > CreateFromMethod()
        {
            var idata = Expression.Parameter(typeof(IData));
            var data  = Expression.Variable(typeof(Data <>).MakeGenericType(Type2));
            var value = Expression.Variable(typeof(T));

            List <Expression> list = new List <Expression>();

            if (TransformerHelper.IsEqualsTypes(Type1, Type2))
            {
                list.Add(Expression.Label(Expression.Label(Type1), Expression.Convert(idata, data.Type).Value()));
            }
            else
            {
                list.Add(Expression.Assign(data, Expression.Convert(idata, data.Type)));
                list.Add(TransformerHelper.BuildBody(value, data.Value(), MembersOrder1, MembersOrder2));
                list.Add(Expression.Label(Expression.Label(Type1), value));
            }

            return(Expression.Lambda <Func <IData, T> >(TransformerHelper.IsEqualsTypes(Type1, Type2) ? list[0] : Expression.Block(Type1, new ParameterExpression[] { data, value }, list), idata));
        }