コード例 #1
0
        public Action <string, TItem> Generate()
        {
            var        expressions     = new List <Expression>();
            Expression ownerExpression = _itemParam;

            while (_propertyInfos.Count > 1)
            {
                var property           = _propertyInfos.Pop();
                var propertyExpression = Expression.Property(ownerExpression, property);
                var ifNullCreate       = Expression.IfThen(Expression.Equal(propertyExpression, Expression.Constant(null, property.PropertyType)),
                                                           Expression.Assign(propertyExpression,
                                                                             Expression.New(property.PropertyType)));
                expressions.Add(ifNullCreate);
                ownerExpression = propertyExpression;
            }
            var actualProperty           = _propertyInfos.Pop();
            var actualPropertyExpression = Expression.Property(ownerExpression, actualProperty);

            var assignExpression = Expression.Assign(actualPropertyExpression,
                                                     ConverterExpression.Create(actualProperty.PropertyType, _stringParam));

            expressions.Add(assignExpression);

            var block = Expression.Block(expressions);

            return(Expression.Lambda <Action <string, TItem> >(block, _stringParam, _itemParam).Compile());
        }
コード例 #2
0
 public Action <string, TItem> Generate()
 {
     return(Compile(Expression.Invoke(_assignmentLambda, _itemParam, ConverterExpression.Create(_assignmentLambda.Parameters[1].Type, _stringParam))));
 }