コード例 #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();
        }
コード例 #2
0
        static FormatSelector()
        {
            foreach (var type in typeof(FormatSelector).Assembly.GetTypes())
            {
                foreach (var cad in type.GetCustomAttributesData())
                {
                    if (cad.AttributeType != typeof(FormatProxyAttribute))
                    {
                        continue;
                    }
                    if (cad.ConstructorArguments.Count != 1)
                    {
                        continue;
                    }
                    var proxyOf = cad.ConstructorArguments[0].Value as Type;
                    if (proxyOf == null)
                    {
                        continue;
                    }
                    if (!FormatDefinitions.TryGetValue(proxyOf, out var directives))
                    {
                        directives = new List <FormatDirective>();
                        FormatDefinitions.Add(proxyOf, directives);
                    }

                    FormatGenerator.Generate(type, directives);
                }
            }
        }