当前位置: 首页>>代码示例>>C#>>正文


C# MethodDeclarationSyntax.WithReturnType方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.WithReturnType方法的典型用法代码示例。如果您正苦于以下问题:C# MethodDeclarationSyntax.WithReturnType方法的具体用法?C# MethodDeclarationSyntax.WithReturnType怎么用?C# MethodDeclarationSyntax.WithReturnType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax的用法示例。


在下文中一共展示了MethodDeclarationSyntax.WithReturnType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FixMethod

        private SyntaxNode FixMethod(
            bool keepVoid, IMethodSymbol methodSymbol, MethodDeclarationSyntax method,
            ITypeSymbol taskType, INamedTypeSymbol taskOfTType)
        {
            var newReturnType = method.ReturnType;

            if (methodSymbol.ReturnsVoid)
            {
                if (!keepVoid)
                {
                    newReturnType = taskType.GenerateTypeSyntax();
                }
            }
            else
            {
                if (!IsTaskLike(methodSymbol.ReturnType, taskType, taskOfTType))
                {
                    // If it's not already Task-like, then wrap the existing return type
                    // in Task<>.
                    newReturnType = taskOfTType.Construct(methodSymbol.ReturnType).GenerateTypeSyntax();
                }
            }

            var newModifiers = method.Modifiers.Add(s_asyncToken);
            return method.WithReturnType(newReturnType).WithModifiers(newModifiers);
        }
开发者ID:tvsonar,项目名称:roslyn,代码行数:26,代码来源:CSharpMakeMethodAsynchronousCodeFixProvider.cs

示例2: Convert2Task

        private async Task<Document> Convert2Task(Document document, MethodDeclarationSyntax methodDecl, CancellationToken cancellationToken)
        {
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDecl.ReturnType.GetTrailingTrivia());
            var newMethodDecl = methodDecl.WithReturnType(newType);

            var root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
            var newRoot = root.ReplaceNode(methodDecl, newMethodDecl);
            return document.WithSyntaxRoot(newRoot);
        }
开发者ID:modulexcite,项目名称:AsyncFixer,代码行数:9,代码来源:FireForgetFixer.cs

示例3: VoidToTaskAsync

        private async Task<Document> VoidToTaskAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            // The Task object must be parsed from a string using the Syntax Factory
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia());

            var newMethodDeclaration = methodDeclaration.WithReturnType(newType);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            // Return document with transformed tree.
            return newDocument;
        }
开发者ID:jerriclynsjohn,项目名称:roslyn,代码行数:14,代码来源:AsyncVoidCodeFix.cs

示例4: FixMethod

        private SyntaxNode FixMethod(IMethodSymbol methodSymbol, MethodDeclarationSyntax method, ITypeSymbol taskType, ITypeSymbol taskOfTType)
        {
            var newReturnType = method.ReturnType;

            // If the return type is Task<T>, then make the new return type "T".
            // If it is Task, then make the new return type "void".
            if (methodSymbol.ReturnType.OriginalDefinition.Equals(taskType))
            {
                newReturnType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)).WithTriviaFrom(method.ReturnType);
            }
            else if (methodSymbol.ReturnType.OriginalDefinition.Equals(taskOfTType))
            {
                newReturnType = methodSymbol.ReturnType.GetTypeArguments()[0].GenerateTypeSyntax().WithTriviaFrom(method.ReturnType);
            }

            var asyncTokenIndex = method.Modifiers.IndexOf(SyntaxKind.AsyncKeyword);
            SyntaxTokenList newModifiers;
            if (asyncTokenIndex == 0)
            {
                // Have to move the trivia on teh async token appropriately.
                var asyncLeadingTrivia = method.Modifiers[0].LeadingTrivia;

                if (method.Modifiers.Count > 1)
                {
                    // Move the trivia to the next modifier;
                    newModifiers = method.Modifiers.Replace(
                        method.Modifiers[1],
                        method.Modifiers[1].WithPrependedLeadingTrivia(asyncLeadingTrivia));
                    newModifiers = newModifiers.RemoveAt(0);
                }
                else
                {
                    // move it to the return type.
                    newModifiers = method.Modifiers.RemoveAt(0);
                    newReturnType = newReturnType.WithPrependedLeadingTrivia(asyncLeadingTrivia);
                }
            }
            else
            {
                newModifiers = method.Modifiers.RemoveAt(asyncTokenIndex);
            }

            return method.WithReturnType(newReturnType).WithModifiers(newModifiers);
        }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:44,代码来源:CSharpMakeMethodSynchronousCodeFixProvider.cs

示例5: HandleMethodDeclaration

        private static SyntaxNode HandleMethodDeclaration(MethodDeclarationSyntax node)
        {
            TypeSyntax type = node.ReturnType;
            if (type == null || type.IsMissing)
            {
                return null;
            }

            SyntaxTokenList modifiers = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref type, SyntaxKind.PrivateKeyword);
            return node
                .WithReturnType(type)
                .WithModifiers(modifiers)
                .WithoutFormatting();
        }
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:14,代码来源:SA1400CodeFixProvider.cs

示例6: ConvertToAsyncFunction

		private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
		{
			return methodDeclaration.WithReturnType(
				SyntaxFactory.ParseTypeName("Task")
				.WithLeadingTrivia(methodDeclaration.ReturnType.GetLeadingTrivia())
				.WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia()));
		}
开发者ID:FreeBSD-DotNet,项目名称:monodevelop,代码行数:7,代码来源:CSharpConvertToAsyncMethodCodeFixProvider.cs

示例7: MethodReturnType

 // set method return type to returnType
 protected internal static SyntaxNode MethodReturnType(MethodDeclarationSyntax methodDeclaration, string returnType)
 {
     TypeSyntax voidType = SyntaxFactory.ParseTypeName(returnType).WithTrailingTrivia(SyntaxFactory.Whitespace(" "));
     methodDeclaration = methodDeclaration.WithReturnType(voidType);
     return methodDeclaration;
 }
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:7,代码来源:CodeFixProvider.cs

示例8: rewriteEventHandler

        private SyntaxNode rewriteEventHandler(MethodDeclarationSyntax node)
        {
            var code = (BlockSyntax)VisitBlock(node.Body);
            var args = node.ParameterList.ReplaceNodes(node.ParameterList.Parameters, (oldParam, newParam) =>
            {
                if (oldParam.Identifier.IsMissing)
                    return newParam.WithType(Void).
                                    WithIdentifier(SyntaxFactory.Identifier(newParam.Type.ToString()));

                return newParam;
            });

            var result = node.WithReturnType(Void).
                              WithParameterList(args).
                              WithModifiers(Private).
                              WithBody(code);

            return ctx_.AddLinker(result, (ctx, linkNode, newNode, model) =>
            {
                MethodDeclarationSyntax mthd   = (MethodDeclarationSyntax)linkNode;
                MethodDeclarationSyntax output = (MethodDeclarationSyntax)newNode;

                ParameterListSyntax methdArgs = mthd.ParameterList;

                string methodName = "on_" + mthd.Identifier.ToString();

                ISymbol     self = model.GetDeclaredSymbol(mthd);
                ITypeSymbol type = (ITypeSymbol)self.ContainingSymbol;

                string evName   = mthd.Identifier.ToString();
                string typeName = type.Name;
                bool   found    = false;
                while (type != null && !found)
                {
                    foreach (var ev in type.GetMembers().OfType<IEventSymbol>())
                    {
                        if (matchEventName(ev.Name, evName))
                        {
                            //arguments
                            foreach (var syntax in ev.Type.DeclaringSyntaxReferences)
                            {
                                var refNode = (DelegateDeclarationSyntax)syntax.GetSyntax();

                                int  pCount = methdArgs.Parameters.Count;
                                int  idx    = 0;
                                bool match  = true;
                                args = refNode.ParameterList.ReplaceNodes(refNode.ParameterList.Parameters, (oldArg, newArg) =>
                                {
                                    if (match)
                                    {
                                        if (idx >= pCount && match)
                                            return newArg;

                                        ParameterSyntax arg = methdArgs.Parameters[idx++];
                                        string argName = arg.Identifier.ToString();
                                        if (argName == oldArg.Identifier.ToString())
                                        {
                                            //coincident parameters, fix missing type or return same
                                            if (arg.Identifier.IsMissing || arg.Type.ToString() == "void")
                                                return newArg.WithIdentifier(arg.Identifier);

                                            return arg;
                                        }
                                        else
                                        {
                                            match = false;
                                            if (!refNode.ParameterList.Parameters.Any(p => p.Identifier.ToString().Equals(arg.Identifier.ToString())))
                                            {
                                                //name change?
                                                if (oldArg.Identifier.IsMissing)
                                                    return newArg.WithIdentifier(SyntaxFactory.Identifier(arg.Type.ToString()));

                                                return arg;
                                            }
                                        }
                                    }

                                    return newArg;
                                });
                            }

                            //register event initialization
                            ctx.AddTypeInfo(typeName, "initializer", EventInitializer(ev.Name, methodName));
                            found = true;
                            break;
                        }
                    }

                    type = type.BaseType;
                }

                if (!found)
                {
                    //td: error, no such event
                }

                return output.WithIdentifier(SyntaxFactory.Identifier(methodName)).
                              WithParameterList(args);
            });
        }
开发者ID:mpmedia,项目名称:Excess,代码行数:100,代码来源:Compiler.cs

示例9: ConvertToAsyncFunction

 private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
 {
     return methodDeclaration.WithReturnType(
         SyntaxFactory.ParseTypeName("Task")
             .WithTriviaFrom(methodDeclaration));
 }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:6,代码来源:CSharpConvertToAsyncMethodCodeFixProvider.cs


注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.WithReturnType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。