本文整理汇总了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);
}
示例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);
}
示例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;
}
示例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);
}
示例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();
}
示例6: ConvertToAsyncFunction
private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
{
return methodDeclaration.WithReturnType(
SyntaxFactory.ParseTypeName("Task")
.WithLeadingTrivia(methodDeclaration.ReturnType.GetLeadingTrivia())
.WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia()));
}
示例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;
}
示例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);
});
}
示例9: ConvertToAsyncFunction
private MethodDeclarationSyntax ConvertToAsyncFunction(MethodDeclarationSyntax methodDeclaration)
{
return methodDeclaration.WithReturnType(
SyntaxFactory.ParseTypeName("Task")
.WithTriviaFrom(methodDeclaration));
}