本文整理汇总了C#中ExpressionSyntax.IsAnyLiteralExpression方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.IsAnyLiteralExpression方法的具体用法?C# ExpressionSyntax.IsAnyLiteralExpression怎么用?C# ExpressionSyntax.IsAnyLiteralExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.IsAnyLiteralExpression方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsTypeApparentInAssignmentExpression
/// <summary>
/// Analyzes if type information is obvious to the reader by simply looking at the assignment expression.
/// </summary>
/// <remarks>
/// <paramref name="typeInDeclaration"/> accepts null, to be able to cater to codegen features
/// that are about to generate a local declaration and do not have this information to pass in.
/// Things (like analyzers) that do have a local declaration already, should pass this in.
/// </remarks>
public static bool IsTypeApparentInAssignmentExpression(
TypeStylePreference stylePreferences,
ExpressionSyntax initializerExpression,
SemanticModel semanticModel,
CancellationToken cancellationToken,
ITypeSymbol typeInDeclaration = null)
{
// default(type)
if (initializerExpression.IsKind(SyntaxKind.DefaultExpression))
{
return true;
}
// literals, use var if options allow usage here.
if (initializerExpression.IsAnyLiteralExpression())
{
return stylePreferences.HasFlag(TypeStylePreference.ImplicitTypeForIntrinsicTypes);
}
// constructor invocations cases:
// = new type();
if (initializerExpression.IsKind(SyntaxKind.ObjectCreationExpression) &&
!initializerExpression.IsKind(SyntaxKind.AnonymousObjectCreationExpression))
{
return true;
}
// explicit conversion cases:
// (type)expr, expr is type, expr as type
if (initializerExpression.IsKind(SyntaxKind.CastExpression) ||
initializerExpression.IsKind(SyntaxKind.IsExpression) ||
initializerExpression.IsKind(SyntaxKind.AsExpression))
{
return true;
}
// other Conversion cases:
// a. conversion with helpers like: int.Parse methods
// b. types that implement IConvertible and then invoking .ToType()
// c. System.Convert.Totype()
var memberName = GetRightmostInvocationExpression(initializerExpression).GetRightmostName();
if (memberName == null)
{
return false;
}
var methodSymbol = semanticModel.GetSymbolInfo(memberName, cancellationToken).Symbol as IMethodSymbol;
if (methodSymbol == null)
{
return false;
}
if (memberName.IsRightSideOfDot())
{
var containingTypeName = memberName.GetLeftSideOfDot();
return IsPossibleCreationOrConversionMethod(methodSymbol, typeInDeclaration, semanticModel, containingTypeName, cancellationToken);
}
return false;
}