Ejemplo n.º 1
0
        internal static async Task TestAddConversionAsync(
            string initial,
            string expected,
            Type toType,
            Func <SemanticModel, IParameterSymbol> fromType,
            bool isImplicit                = false,
            Accessibility accessibility    = Accessibility.Public,
            DeclarationModifiers modifiers = default(DeclarationModifiers),
            string statements              = null,
            CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
            bool compareTokens = true)
        {
            if (statements != null)
            {
                expected = expected.Replace("$$", statements);
            }

            using (var context = await TestContext.CreateAsync(initial, expected, compareTokens))
            {
                var parsedStatements = context.ParseStatements(statements);
                var method           = CodeGenerationSymbolFactory.CreateConversionSymbol(
                    null,
                    accessibility,
                    modifiers,
                    GetTypeSymbol(toType)(context.SemanticModel),
                    fromType(context.SemanticModel),
                    isImplicit,
                    parsedStatements);

                context.Result = await context.Service.AddMethodAsync(context.Solution, (INamedTypeSymbol)context.GetDestination(), method, codeGenerationOptions);
            }
        }
        private static async Task <ISymbol?> TryRehydrateAsync(Document document, CompletionItem item, CancellationToken cancellationToken)
        {
            // If we're need to rehydrate the conversion, pull out the necessary parts.
            if (item.Properties.ContainsKey(RehydrateName))
            {
                var symbols = await SymbolCompletionItem.GetSymbolsAsync(item, document, cancellationToken).ConfigureAwait(false);

                if (symbols.Length == 3 &&
                    symbols[0] is INamedTypeSymbol containingType &&
                    symbols[1] is ITypeSymbol fromType &&
                    symbols[2] is ITypeSymbol toType)
                {
                    return(CodeGenerationSymbolFactory.CreateConversionSymbol(
                               toType: toType,
                               fromType: CodeGenerationSymbolFactory.CreateParameterSymbol(fromType, "value"),
                               containingType: containingType,
                               documentationCommentXml: item.Properties[DocumentationCommentXmlName]));
                }

                return(null);
            }
            else
            {
                // Otherwise, just go retrieve the conversion directly.
                var symbols = await SymbolCompletionItem.GetSymbolsAsync(item, document, cancellationToken).ConfigureAwait(false);

                return(symbols.Length == 1 ? symbols.Single() : null);
            }
        }