Exemplo n.º 1
0
        private static void setValue(object dest, ListBinding b, object val)
        {
            object list = getPropertyValue(dest, b.ListName);

            if (list == null)
            {
                return;
            }
            Type itemType = dest.GetType().GetProperty(b.ListName).PropertyType.GetProperty("Item").PropertyType;

            var newobj = Expression.Lambda(Expression.New(itemType)).Compile().DynamicInvoke();

            setPropValue(newobj, b.PropName, val);

            if (b.ListFilterOrInits != null && b.ListFilterOrInits.Count > 0)
            {
                foreach (var listInit in b.ListFilterOrInits)
                {
                    setPropValue(newobj, listInit.PropName, listInit.PropValue);
                }
            }

            var addMethod = dest.GetType().GetProperty(b.ListName).PropertyType.GetMethod("Add", new[] { itemType });

            // ReSharper disable once PossiblyMistakenUseOfParamsMethod
            var l1 = Expression.Lambda(Expression.Call(Expression.Constant(list), addMethod, Expression.Constant(newobj)));

            l1.Compile().DynamicInvoke();
        }
Exemplo n.º 2
0
        private static object getValue(object o, ListBinding b)
        {
            var list = getPropertyValue(o, b.ListName) as IEnumerable <object>;

            if (list == null)
            {
                return(null);
            }

            var  filteredList = new List <object>();
            Type itemType     = o.GetType().GetProperty(b.ListName).PropertyType.GetProperty("Item").PropertyType;

            foreach (var listItem in list)
            {
                bool isCanAdd = true;
                if (b.ListFilterOrInits != null && b.ListFilterOrInits.Count > 0)
                {
                    foreach (var listFilter in b.ListFilterOrInits)
                    {
                        var        param      = Expression.Parameter(itemType, "x");
                        Expression expression = getExpression(listFilter.PropName, param);
                        expression = Expression.Call(expression, itemType.GetMethod("ToString"));
                        if (listFilter.Condition == "equal")
                        {
                            expression = Expression.Equal(expression, Expression.Constant(listFilter.PropValue));
                        }
                        var res = (bool)Expression.Lambda(expression, param).Compile().DynamicInvoke(listItem);
                        if (!res)
                        {
                            isCanAdd = false;
                            break;
                        }
                    }
                }
                if (isCanAdd)
                {
                    filteredList.Add(listItem);
                }
            }
            if (filteredList.Count > b.ItemIndex)
            {
                return(getPropertyValue(filteredList[b.ItemIndex], b.PropName));
            }
            return(null);
        }