Exemplo n.º 1
0
        public override Expression Bind(object[] args, ReadOnlyCollection <ParameterExpression> parameters, LabelTarget returnLabel)
        {
            if (args.Length == 1)
            {
                Debug.Assert(this == Instance0, "Use Instance0 when passing 0 args");

                var criteria = new FormatSelectionCriteria();
                if (args[0] is PSObject psobj)
                {
                    criteria.Type = psobj.BaseObject.GetType();
                }
                else
                {
                    criteria.Type = args[0].GetType();
                }

                var directive = FormatSelector.FindDirective(criteria) ?? FormatGenerator.Generate(criteria.Type);

                return(directive.Bind(parameters[0], args[0].GetType(), Expression.Constant(directive), returnLabel));
            }

            if (args.Length == 2)
            {
                Debug.Assert(this == Instance1, "Use Instance0 when passing 0 args");

                switch (args[1])
                {
                case ListFormat listFormat:
                    return(listFormat.Bind(parameters[0], args[0].GetType(), parameters[1], returnLabel));
                }
            }

            throw new Exception();
        }
Exemplo n.º 2
0
        public static FormatDirective FindDirective(FormatSelectionCriteria criteria)
        {
            var type = criteria.Type;

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

                    if (directives != null)
                    {
                        foreach (var directive in directives)
                        {
                            switch (criteria.Style)
                            {
                            case FormatStyle.List:
                                if (!(directive is ListFormat))
                                {
                                    continue;
                                }
                                break;

                            case FormatStyle.Table:
                                //if (!(directive is TableFormat)) continue;
                                break;
                            }

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

                            return(directive);
                        }
                    }

                    type = type.BaseType;
                }
            }

            return(null);
        }