Beispiel #1
0
        public static Expression <Func <IFields, TDestiny> > GetMemberInit <TDestiny>(IFields fields, bool useXmlAttributes = true)
            where TDestiny : new()
        {
            var typeOfDestiny = typeof(TDestiny);
            var props         = ObjectHelpers.LoadProperties(typeOfDestiny, p => p.CanWrite, useXmlAttributes);

            var typeOfFields  = typeof(IFields);
            var methodField   = typeOfFields.GetMethod("Item");
            var propertyValue = typeof(IField).GetProperty("Value");

            var count       = fields.Count;
            var paramSource = Expression.Parameter(typeOfFields, "fields");
            var bindings    = new List <MemberBinding>(count);

            for (var i = 0; i < count; ++i)
            {
                var          field = fields.Item(i);
                PropertyInfo propertyInfo;

                if (!props.TryGetValue(field.Name, out propertyInfo))
                {
                    continue;
                }

                MemberBinding binding;
                var           value = Expression.Property(
                    Expression.Call(paramSource, methodField, Expression.Constant(i, ObjectHelpers.TypeOfObject)), propertyValue);

                if (propertyInfo.PropertyType.Name == "Boolean" && field.Type == BoFieldTypes.db_Alpha && field.Size == 1)
                {
                    binding = Expression.Bind(
                        propertyInfo,
                        Expression.Equal(Expression.Convert(value, ObjectHelpers.TypeOfString), Expression.Constant("Y")));
                    //binding = Expression.Bind(propertyInfo, Expression.Condition(
                    //	Expression.Equal(
                    //		Expression.Convert(value, typeOfString),
                    //		Expression.Constant("Y")),
                    //	Expression.Constant(true),
                    //	Expression.Constant(false)));
                }
                else
                {
                    binding = Expression.Bind(propertyInfo, Expression.Convert(value, propertyInfo.PropertyType));
                }

                bindings.Add(binding);
            }

            var lambda = Expression.Lambda <Func <IFields, TDestiny> >(
                Expression.MemberInit(Expression.New(typeOfDestiny), bindings),
                new[] { paramSource });

            return(lambda);
        }