本文整理汇总了C#中InvocationExpressionSyntax.GetReferencedSymbol方法的典型用法代码示例。如果您正苦于以下问题:C# InvocationExpressionSyntax.GetReferencedSymbol方法的具体用法?C# InvocationExpressionSyntax.GetReferencedSymbol怎么用?C# InvocationExpressionSyntax.GetReferencedSymbol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类InvocationExpressionSyntax
的用法示例。
在下文中一共展示了InvocationExpressionSyntax.GetReferencedSymbol方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitInvocationExpression
/// <summary>
/// Adds omitted optional arguments to the <paramref name="invocation" />.
/// </summary>
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax invocation)
{
// Nothing to do if the method doesn't declare any optional parameters
var methodSymbol = invocation.GetReferencedSymbol<IMethodSymbol>(SemanticModel);
if (methodSymbol.Parameters.All(parameter => !parameter.IsOptional))
return base.VisitInvocationExpression(invocation);
// Nothing to do if all optional arguments have been provided
if (invocation.ArgumentList.Arguments.Count == methodSymbol.Parameters.Length)
return base.VisitInvocationExpression(invocation);
// Determine the optional parameters that the invocation omits
var omittedParameters = new HashSet<IParameterSymbol>(methodSymbol.Parameters);
omittedParameters.ExceptWith(invocation.ArgumentList.Arguments.Select(argument => argument.GetParameterSymbol(SemanticModel)));
// Recursively normalize the arguments of the invocation
invocation = (InvocationExpressionSyntax)base.VisitInvocationExpression(invocation);
var arguments = invocation.ArgumentList.Arguments;
// Add the default values of all omitted optional arguments as named arguments to the end of the argument list;
// we can safely ignore param arrays here
foreach (var omittedParameter in omittedParameters.Where(omittedParameter => !omittedParameter.IsParams))
{
Assert.That(omittedParameter.HasExplicitDefaultValue, "Expected an implicit default value.");
Assert.That(omittedParameter.RefKind == RefKind.None, "Optional parameter cannot be ref or out.");
// Determine the default value; only structs, decimal, int, and bool are supported
var defaultValue = String.Empty;
var value = omittedParameter.ExplicitDefaultValue;
if (value == null)
defaultValue = String.Format("default({0})", omittedParameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
else if (value is decimal)
defaultValue = ((decimal)value).ToString(CultureInfo.InvariantCulture) + "m";
else if (value is int)
defaultValue = ((int)value).ToString(CultureInfo.InvariantCulture);
else if (value is bool)
defaultValue = ((bool)omittedParameter.ExplicitDefaultValue) ? "true" : "false";
var argument = SyntaxFactory.Argument(SyntaxFactory.NameColon(omittedParameter.Name),
SyntaxFactory.Token(SyntaxKind.None), SyntaxFactory.ParseExpression(defaultValue));
arguments = arguments.Add(argument);
}
return invocation.WithArgumentList(invocation.ArgumentList.WithArguments(arguments)).NormalizeWhitespace();
}
示例2: DecomposeTransitionChain
/// <summary>
/// Collects all calls to <see cref="StateMachine{TState}" /> <c>Transition</c> methods within
/// <paramref name="expression" />.
/// </summary>
private List<Transition> DecomposeTransitionChain(InvocationExpressionSyntax expression, out ExpressionSyntax stateMachine)
{
var transitions = new List<Transition>();
while (true)
{
AddTransitions(transitions, expression.ArgumentList);
var memberAccess = (MemberAccessExpressionSyntax)expression.Expression;
if (memberAccess.Expression.Kind() != SyntaxKind.InvocationExpression)
{
stateMachine = memberAccess.Expression;
break;
}
expression = (InvocationExpressionSyntax)memberAccess.Expression;
var methodSymbol = expression.GetReferencedSymbol<IMethodSymbol>(SemanticModel);
if (!methodSymbol.IsTransitionMethod(SemanticModel))
{
stateMachine = expression;
break;
}
}
transitions.Reverse();
return transitions;
}