Пример #1
0
    public static Expression <Func <complex_type_entity, ComplexType> > CreateExpression()
    {
        var expr = SimpleType.CreateExpression();

        return(arg => new ComplexType
        {
            Id = arg.id,
            Property1 = expr.Compile()(arg.property1),
            Property2 = expr.Compile()(arg.property2)
        });
    }
    public static Expression <Func <complex_type_entity, ComplexType> > CreateExpression()
    {
        var parameterExp = Expression.Parameter(typeof(complex_type_entity), "p0");

        return(Expression.Lambda <Func <complex_type_entity, ComplexType> >(
                   Expression.MemberInit(
                       Expression.New(typeof(ComplexType)),
                       Expression.Bind(typeof(ComplexType).GetProperty("Id"), Expression.Property(parameterExp, "id")),
                       Expression.Bind(typeof(ComplexType).GetProperty("Property1"), SimpleType.CreateExpression(Expression.Property(parameterExp, "simple_type_entity"))),
                       Expression.Bind(typeof(ComplexType).GetProperty("Property2"), SimpleType.CreateExpression(Expression.Property(parameterExp, "simple_type_entity1")))),
                   parameterExp));
    }
    private static Expression CreateNewSimpleTypeExpression(ParameterExpression parameterExp, string simpleTypeProperty)
    {
        var simpleTypeExpr = (MemberInitExpression)SimpleType.CreateExpression().Body;

        MemberBinding[] bindings = new MemberBinding[simpleTypeExpr.Bindings.Count];
        for (int i = 0; i < bindings.Length; i++)
        {
            var oriBinding = simpleTypeExpr.Bindings[i];
            var memberName = ((MemberExpression)((MemberAssignment)oriBinding).Expression).Member.Name;
            bindings[i] = Expression.Bind(oriBinding.Member, Expression.Property(Expression.Property(parameterExp, simpleTypeProperty), memberName));
        }
        return(Expression.MemberInit(simpleTypeExpr.NewExpression, bindings));
    }