Exemplo n.º 1
0
        /// <summary>
        ///     Get the <see cref="ExpressionSyntax"/> for the given parameter.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The <see cref="ExpressionSyntax"/>.</returns>
        private ExpressionSyntax GetValueFactory(ParameterInfo parameter)
        {
            var type    = parameter.ParameterType;
            var context = new RoslynExpressionContext(parameter, ArgumentContext);

            if (ValueFactories.TryGetValue(type, out var value))
            {
                return(value(context));
            }

            foreach (var valueProvider in _valueProviders)
            {
                if (!valueProvider.CanProvide(type))
                {
                    continue;
                }

                foreach (var assembly in valueProvider.GetAssemblies(type))
                {
                    if (!_assemblies.Contains(assembly.Location))
                    {
                        _assemblies.Add(assembly.Location);
                    }
                }

                return(valueProvider.GetRoslynExpression(context));
            }

            return(context.GetService(type));
        }
        public ExpressionSyntax GetRoslynExpression(RoslynExpressionContext context)
        {
            var manager     = context.GetService(typeof(IGuildOptionsManager));
            var optionsType = context.ExpectedType.GetGenericArguments()[0];
            var typeSyntax  = S.ParseTypeName(optionsType.FullName.Replace('+', '.'));
            var getName     = S.GenericName(nameof(SettingExtensions.GetOptionsAsync)).WithTypeArgumentList(S.TypeArgumentList().AddArguments(typeSyntax));
            var get         = S.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, manager, getName);
            var guildId     = S.BinaryExpression(
                SyntaxKind.CoalesceExpression,
                context.GetRequestProperty(nameof(MessageRequest.GuildId)),
                S.ThrowExpression(S.ObjectCreationExpression(
                                      S.IdentifierName("InvalidOperationException"),
                                      S.ArgumentList().AddArguments(S.Argument(S.LiteralExpression(SyntaxKind.StringLiteralExpression, S.Literal(GuildIdNotDefined)))),
                                      null
                                      ))
                );
            var invoke = S.InvocationExpression(get, S.ArgumentList().AddArguments(S.Argument(guildId)));

            return(S.AwaitExpression(invoke));
        }