Exemple #1
0
        internal override Expression Bind(Expression toFormat, Type toFormatType, Expression directive, LabelTarget returnLabel)
        {
            int maxLabel = -1;

            foreach (var entry in Entries)
            {
                var label = entry.Label ?? entry.PropertyName;
                maxLabel = Math.Max(maxLabel, label.Length);
            }
            var formatExpr  = "{0,-" + maxLabel + "} : {1}";
            var expressions = new Expression[Entries.Count];

            for (var i = 0; i < Entries.Count; i++)
            {
                var entry  = Entries[i];
                var binder = FormatGetMemberBinder.Get(entry.PropertyName);
                expressions[i] = Expression.Call(FormatLineMethodInfo,
                                                 Expression.Constant(formatExpr),
                                                 Expression.Constant(entry.PropertyName),
                                                 Expression.Dynamic(binder, typeof(object), toFormat));
            }

            return(Expression.IfThen(
                       Expression.AndAlso(
                           Expression.TypeEqual(toFormat, toFormatType),
                           Expression.Call(Expression.Constant(this), ListFormatEquals, directive)),
                       Expression.Return(returnLabel,
                                         Expression.NewArrayInit(typeof(string), expressions))));
        }
Exemple #2
0
        Expression BindDescriptor(
            ListDescriptor descriptor,
            Expression toFormat,
            Expression criteria,
            LabelTarget returnLabel)
        {
            Expression GetPropertyBinding(ListDescriptorEntry entry)
            {
                switch (entry)
                {
                case ListDescriptorPropertyEntry ldpe:
                    var binder = FormatGetMemberBinder.Get(ldpe.PropertyName);
                    return(Expression.Dynamic(binder, typeof(object), toFormat));

                case ListDescriptorComputedPropertyEntry ldcpe:
                    throw new NotImplementedException();
                }

                throw new InvalidOperationException();
            }

            int maxLabel = -1;

            foreach (var entry in descriptor.Entries)
            {
                var label = entry.GetLabel();
                maxLabel = Math.Max(maxLabel, label.Length);
            }
            var formatExpr  = "{0,-" + maxLabel + "} : {1}";
            var expressions = new Expression[descriptor.Entries.Count];

            for (var i = 0; i < expressions.Length; i++)
            {
                var entry = descriptor.Entries[i];
                expressions[i] = Expression.Call(GetPropertyLineFormattedResultMethodInfo,
                                                 Expression.Constant(formatExpr),
                                                 Expression.Constant(entry.GetLabel()),
                                                 GetPropertyBinding(entry));
            }

            return(Expression.IfThen(
                       SelectionCriteria.GetCompatibleCall(criteria, descriptor, toFormat),
                       Expression.Return(returnLabel,
                                         Expression.NewArrayInit(typeof(FormatInstruction), expressions))));
        }
Exemple #3
0
        Expression BindDescriptor(
            TableDescriptor descriptor,
            Expression toFormat,
            Expression criteria,
            LabelTarget returnLabel)
        {
            var expressions = new Expression[descriptor.Columns.Count];

            for (var i = 0; i < expressions.Length; i++)
            {
                var col = descriptor.Columns[i];
                if (col.Method != null)
                {
                    var type = descriptor.Type;
                    expressions[i] = Expression.Dynamic(
                        ToStringBinder.Instance,
                        typeof(string),
                        Expression.TryCatch(
                            Expression.Call(col.Method,
                                            Expression.Convert(
                                                Expression.Property(
                                                    Expression.Convert(toFormat, typeof(PSObject)), "BaseObject"),
                                                type)),
                            Expression.Catch(typeof(Exception), Expression.Default(col.Method.ReturnType))));
                    continue;
                }

                var binder = FormatGetMemberBinder.Get(col.Property);
                expressions[i] = Expression.Dynamic(
                    ToStringBinder.Instance,
                    typeof(string),
                    Expression.Dynamic(binder, typeof(object), toFormat));
            }

            return(Expression.IfThen(
                       SelectionCriteria.GetCompatibleCall(criteria, descriptor, toFormat),
                       Expression.Return(returnLabel,
                                         Expression.Call(NewEmitTableRowMethodInfo,
                                                         Expression.Constant(descriptor),
                                                         Expression.NewArrayInit(typeof(string), expressions)))));
        }