Пример #1
0
        public static Descriptor FindDirective(SelectionCriteria criteria, object o)
        {
            var type = criteria.Type;

            lock (FormatDefinitions)
            {
                while (type != null)
                {
                    if (!FormatDefinitions.TryGetValue(type, out var descriptors))
                    {
                        if (type.IsGenericType)
                        {
                            // Try again with the unspecialized type
                            var generic = type.GetGenericTypeDefinition();
                            FormatDefinitions.TryGetValue(generic, out descriptors);
                        }
                    }

                    if (descriptors != null)
                    {
                        foreach (var descriptor in descriptors)
                        {
                            switch (criteria.Style)
                            {
                            case Style.List:
                                if (!(descriptor is ListDescriptor))
                                {
                                    continue;
                                }
                                break;

                            case Style.Table:
                                if (!(descriptor is TableDescriptor))
                                {
                                    continue;
                                }
                                break;
                            }

                            if (descriptor.When != null && !descriptor.When.Applies(o))
                            {
                                continue;
                            }

                            if (!string.IsNullOrEmpty(criteria.Name) &&
                                !criteria.Name.Equals(descriptor.Name, StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            return(descriptor.Clone(criteria.Type));
                        }
                    }

                    type = type.BaseType;
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Format an object using the specified descriptor.
        /// </summary>
        /// <param name="obj">The object to format.</param>
        /// <param name="descriptor">The formatting descriptor.</param>
        internal static IEnumerable <FormatInstruction> Format(object obj, Descriptor descriptor)
        {
            Type type;

            if (obj is PSObject psobj)
            {
                type = psobj.BaseObject.GetType();
            }
            else
            {
                type  = obj.GetType();
                psobj = new PSObject(obj);
            }

            if (descriptor == null)
            {
                descriptor = psobj.Properties[AttachedFormatPropertyName]?.Value as Descriptor;
            }

            Style style = Style.Default;

            if (descriptor != null)
            {
                switch (descriptor)
                {
                case ListDescriptor lf:
                    style = Style.List;
                    // Don't use the descriptor if there are no entries.
                    if (lf.Entries.Count == 0)
                    {
                        descriptor = null;
                    }
                    break;

                case TableDescriptor tf:
                    style = Style.Table;
                    // Don't use the descriptor if there are no entries.
                    if (tf.Columns.Count == 0)
                    {
                        descriptor = null;
                    }
                    break;
                }

                if (descriptor != null && descriptor.Type == null)
                {
                    descriptor = descriptor.Clone(type);
                }
            }

            var criteria = new SelectionCriteria(
                descriptor: descriptor,
                style: style,
                type: type,
                name: null);

            return(_site.Target.Invoke(_site, psobj, criteria));
        }
Пример #3
0
 private Expression BindDescriptor(
     BasicDescriptor descriptor,
     Expression toFormat,
     Expression criteria,
     LabelTarget returnLabel)
 {
     return(Expression.IfThen(
                SelectionCriteria.GetCompatibleCall(criteria, descriptor, toFormat),
                Expression.Return(returnLabel,
                                  Expression.Call(GetBasicFormattedResultMethodInfo, Expression.Convert(toFormat, typeof(object))))));
 }
Пример #4
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))));
        }
Пример #5
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)))));
        }
Пример #6
0
 public static Descriptor Generate(SelectionCriteria criteria)
 {
     return(new BasicDescriptor(criteria.Type));
 }