本文整理汇总了C#中ExpressionSyntax.IsKind方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.IsKind方法的具体用法?C# ExpressionSyntax.IsKind怎么用?C# ExpressionSyntax.IsKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.IsKind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Check
void Check(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax condition)
{
if (condition == null)
return;
if (condition.IsKind(SyntaxKind.TrueLiteralExpression) || condition.IsKind(SyntaxKind.FalseLiteralExpression))
return;
var resolveResult = nodeContext.SemanticModel.GetConstantValue(condition);
if (!resolveResult.HasValue || !(resolveResult.Value is bool))
return;
var value = (bool)resolveResult.Value;
nodeContext.ReportDiagnostic(Diagnostic.Create(
descriptor.Id,
descriptor.Category,
string.Format(descriptor.MessageFormat.ToString(), value),
descriptor.DefaultSeverity,
descriptor.DefaultSeverity,
descriptor.IsEnabledByDefault,
4,
descriptor.Title,
descriptor.Description,
descriptor.HelpLinkUri,
condition.GetLocation(),
null,
new[] { value.ToString() }
));
}
示例2: ToIntegratedString
private ExpressionSyntax ToIntegratedString(ExpressionSyntax left, ExpressionSyntax right)
{
if (left.IsKind(SyntaxKind.AddExpression))
{
var add = left as BinaryExpressionSyntax;
left = ToIntegratedString(add.Left, add.Right);
}
if (right.IsKind(SyntaxKind.AddExpression))
{
var add = right as BinaryExpressionSyntax;
right = ToIntegratedString(add.Left, add.Right);
}
if (left.IsKind(SyntaxKind.StringLiteralExpression) && right.IsKind(SyntaxKind.StringLiteralExpression))
{
var leftToken = ((LiteralExpressionSyntax)left).Token;
var rightToken = ((LiteralExpressionSyntax)right).Token;
var value = leftToken.ValueText + rightToken.ValueText;
var verbatimString = leftToken.Text.StartsWith("@") || rightToken.Text.StartsWith("@");
if (verbatimString)
{
var text = "@\"" + value.Replace("\"", "\"\"") + "\"";
return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(text, value));
}
else
{
return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(value));
}
}
else
{
var leftInterpolatedString = ToInterpolatedString(left);
var rightInterpolatedString = ToInterpolatedString(right);
var leftVerbatimString = leftInterpolatedString.StringStartToken.Text.Contains("@");
var rightVerbatimString = rightInterpolatedString.StringStartToken.Text.Contains("@");
if (leftVerbatimString != rightVerbatimString)
{
if (leftVerbatimString) { rightInterpolatedString = ToVerbatimInterpolatedString(rightInterpolatedString); }
if (rightVerbatimString) { leftInterpolatedString = ToVerbatimInterpolatedString(leftInterpolatedString); }
}
return leftInterpolatedString.AddContents(rightInterpolatedString.Contents.ToArray());
}
}
开发者ID:miya2000,项目名称:StringIntegrationCodeRefactoring,代码行数:48,代码来源:StringIntegrationCodeRefactoringProvider.cs
示例3: 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;
}
示例4: CreateNewComparison
private static ExpressionSyntax CreateNewComparison(ExpressionSyntax comparisonExpression)
{
if (comparisonExpression.IsKind(SyntaxKind.ConditionalExpression))
return SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression,
SyntaxFactory.ParenthesizedExpression(comparisonExpression),
SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
if (comparisonExpression.IsKind(SyntaxKind.LogicalNotExpression))
return ((PrefixUnaryExpressionSyntax)comparisonExpression).Operand;
if (comparisonExpression.IsKind(SyntaxKind.EqualsExpression))
{
var comparisonBinary = (BinaryExpressionSyntax)comparisonExpression;
if (comparisonBinary.Right.IsKind(SyntaxKind.TrueLiteralExpression))
return comparisonBinary.WithRight(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
if (comparisonBinary.Left.IsKind(SyntaxKind.TrueLiteralExpression))
return comparisonBinary.WithLeft(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
if (comparisonBinary.Right.IsKind(SyntaxKind.FalseLiteralExpression))
return comparisonBinary.Left;
if (comparisonBinary.Left.IsKind(SyntaxKind.FalseLiteralExpression))
return comparisonBinary.Right;
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.NotEqualsExpression);
}
if (comparisonExpression.IsKind(SyntaxKind.NotEqualsExpression))
{
var comparisonBinary = (BinaryExpressionSyntax)comparisonExpression;
if (comparisonBinary.Right.IsKind(SyntaxKind.TrueLiteralExpression))
return comparisonBinary.Left;
if (comparisonBinary.Left.IsKind(SyntaxKind.TrueLiteralExpression))
return comparisonBinary.Right;
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.EqualsExpression);
}
if (comparisonExpression.IsKind(SyntaxKind.GreaterThanExpression))
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.LessThanOrEqualExpression);
if (comparisonExpression.IsKind(SyntaxKind.GreaterThanOrEqualExpression))
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.LessThanExpression);
if (comparisonExpression.IsKind(SyntaxKind.LessThanExpression))
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.GreaterThanOrEqualExpression);
if (comparisonExpression.IsKind(SyntaxKind.LessThanOrEqualExpression))
return CreateNewBinaryExpression(comparisonExpression, SyntaxKind.GreaterThanExpression);
if (comparisonExpression.IsKind(SyntaxKind.TrueLiteralExpression))
return SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression);
if (comparisonExpression.IsKind(SyntaxKind.FalseLiteralExpression))
return SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression);
return SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression,
comparisonExpression,
SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
}
示例5: IsPossibleExpression
static bool IsPossibleExpression(ExpressionSyntax left)
{
if (left.IsKind(SyntaxKind.IdentifierName))
return true;
var mr = left as MemberAccessExpressionSyntax;
if (mr == null)
return false;
return mr.Expression is MyClassExpressionSyntax;
}
示例6: CheckTarget
internal static bool CheckTarget(ExpressionSyntax target, ExpressionSyntax expr)
{
if (target.IsKind(SyntaxKind.IdentifierName))
return !expr.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(n => ((IdentifierNameSyntax)target).Identifier.ValueText == n.Identifier.ValueText);
if (target.IsKind(SyntaxKind.SimpleMemberAccessExpression))
return !expr.DescendantNodesAndSelf().Any(
n =>
{
// StartsWith() is a very simple solution, but should be enough in usual cases
if (n.IsKind(SyntaxKind.IdentifierName))
return ((MemberAccessExpressionSyntax)target).Expression.ToString().StartsWith(((IdentifierNameSyntax)n).Identifier.ValueText);
if (n.IsKind(SyntaxKind.SimpleMemberAccessExpression))
return ((MemberAccessExpressionSyntax)target).Expression.ToString().StartsWith(((MemberAccessExpressionSyntax)n).Expression.ToString());
return false;
}
);
return false;
}
示例7: IsAssigningToDefault
private static bool IsAssigningToDefault(ITypeSymbol fieldType, ExpressionSyntax initializerValue, SemanticModel semanticModel)
{
if (fieldType.IsReferenceType)
{
if (!initializerValue.IsKind(SyntaxKind.NullLiteralExpression)) return false;
}
else
{
if (!IsValueTypeAssigningToDefault(fieldType, initializerValue, semanticModel)) return false;
}
return true;
}
示例8: IsLiteralOrMadeOfBinaryOperations
private bool IsLiteralOrMadeOfBinaryOperations(ExpressionSyntax expression)
{
if (expression is BinaryExpressionSyntax)
{
if (!AreLeftAndRightLiterals((BinaryExpressionSyntax)expression)) return false;
}
else if (expression is ParenthesizedExpressionSyntax)
{
var binary = ((ParenthesizedExpressionSyntax)expression).Expression as BinaryExpressionSyntax;
if (binary == null || !AreLeftAndRightLiterals(binary)) return false;
}
else
{
if (!expression.IsKind(SyntaxKind.NumericLiteralExpression)) return false;
}
return true;
}
示例9: ConvertToStatement
private static StatementSyntax ConvertToStatement(
ExpressionSyntax expression,
SyntaxToken semicolonToken,
bool createReturnStatementForExpression)
{
if (expression.IsKind(SyntaxKind.ThrowExpression))
{
var throwExpression = (ThrowExpressionSyntax)expression;
return SyntaxFactory.ThrowStatement(throwExpression.ThrowKeyword, throwExpression.Expression, semicolonToken);
}
else if (createReturnStatementForExpression)
{
return SyntaxFactory.ReturnStatement(expression)
.WithSemicolonToken(semicolonToken);
}
else
{
return SyntaxFactory.ExpressionStatement(expression)
.WithSemicolonToken(semicolonToken);
}
}
示例10: IsLiteral
private static bool IsLiteral(ExpressionSyntax expression, SemanticModel semanticModel)
{
// Default expressions are most of the time constants, but not for default(MyStruct).
if (expression.IsKind(SyntaxKind.DefaultExpression))
{
return true;
}
var constantValue = semanticModel.GetConstantValue(expression);
if (constantValue.HasValue)
{
return true;
}
IFieldSymbol fieldSymbol = semanticModel.GetSymbolInfo(expression).Symbol as IFieldSymbol;
if (fieldSymbol != null)
{
return fieldSymbol.IsStatic && fieldSymbol.IsReadOnly;
}
return false;
}
示例11: IsRethrowOfCaughtException
private static bool IsRethrowOfCaughtException(ExpressionSyntax thrown)
{
var containingCatch = thrown.FirstAncestorOrSelf<CatchClauseSyntax>();
if (containingCatch != null)
{
var catchDecl = containingCatch.Declaration;
if (catchDecl != null)
{
var caughtExId = catchDecl.Identifier;
if (thrown.IsKind(SyntaxKind.IdentifierName) && ((IdentifierNameSyntax)thrown).Identifier.IsEquivalentTo(caughtExId))
{
return true;
}
}
}
return false;
}
示例12: IsConditionThatChecksForNull
private static bool IsConditionThatChecksForNull(ExpressionSyntax condition, SemanticModel semanticModel, ISymbol symbol, SyntaxKind binarySyntaxKind)
{
if (condition == null) return false;
if (!condition.IsKind(binarySyntaxKind)) return false;
var equals = (BinaryExpressionSyntax)condition;
if (equals.Left == null || equals.Right == null) return false;
ISymbol identifierSymbol;
if (equals.Right.IsKind(SyntaxKind.NullLiteralExpression) && equals.Left.IsKind(SyntaxKind.IdentifierName))
identifierSymbol = semanticModel.GetSymbolInfo(equals.Left).Symbol;
else if (equals.Left.IsKind(SyntaxKind.NullLiteralExpression) && equals.Right.IsKind(SyntaxKind.IdentifierName))
identifierSymbol = semanticModel.GetSymbolInfo(equals.Right).Symbol;
else return false;
if (symbol.Equals(identifierSymbol)) return true;
return false;
}
示例13: IsNewTrackedTypeObjectCreation
private static bool IsNewTrackedTypeObjectCreation(ExpressionSyntax expression, SemanticModel semanticModel)
{
if (!expression.IsKind(SyntaxKind.ObjectCreationExpression))
{
return false;
}
var type = semanticModel.GetTypeInfo(expression).Type;
if (!type.IsAny(TrackedTypes))
{
return false;
}
var constructor = semanticModel.GetSymbolInfo(expression).Symbol as IMethodSymbol;
return constructor != null && !constructor.Parameters.Any(p => p.Type.Implements(KnownType.System_IDisposable));
}
示例14: IsMethodUsedAsEventHandlerFunctionOrActionInExpression
private static bool IsMethodUsedAsEventHandlerFunctionOrActionInExpression(IMethodSymbol methodSymbol, ExpressionSyntax expression, SemanticModel semanticModel)
{
return !expression.IsKind(SyntaxKind.InvocationExpression) &&
IsStandaloneExpression(expression) &&
methodSymbol.Equals(semanticModel.GetSymbolInfo(expression).Symbol?.OriginalDefinition);
}
示例15: IsReducedSelectOrGroupByClause
/// <summary>
/// When queries are translated into expressions select and group-by expressions such that
/// 1) select/group-by expression is the same identifier as the "source" identifier and
/// 2) at least one Where or OrderBy clause but no other clause is present in the contained query body or
/// the expression in question is a group-by expression and the body has no clause
///
/// do not translate into lambdas.
/// By "source" identifier we mean the identifier specified in the from clause that initiates the query or the query continuation that includes the body.
///
/// The above condition can be derived from the language specification (chapter 7.16.2) as follows:
/// - In order for 7.16.2.5 "Select clauses" to be applicable the following conditions must hold:
/// - There has to be at least one clause in the body, otherwise the query is reduced into a final form by 7.16.2.3 "Degenerate query expressions".
/// - Only where and order-by clauses may be present in the query body, otherwise a transformation in 7.16.2.4 "From, let, where, join and orderby clauses"
/// produces pattern that doesn't match the requirements of 7.16.2.5.
///
/// - In order for 7.16.2.6 "Groupby clauses" to be applicable the following conditions must hold:
/// - Only where and order-by clauses may be present in the query body, otherwise a transformation in 7.16.2.4 "From, let, where, join and orderby clauses"
/// produces pattern that doesn't match the requirements of 7.16.2.5.
/// </summary>
private static bool IsReducedSelectOrGroupByClause(SelectOrGroupClauseSyntax selectOrGroupClause, ExpressionSyntax selectOrGroupExpression)
{
if (!selectOrGroupExpression.IsKind(SyntaxKind.IdentifierName))
{
return false;
}
var selectorIdentifier = ((IdentifierNameSyntax)selectOrGroupExpression).Identifier;
SyntaxToken sourceIdentifier;
QueryBodySyntax containingBody;
var containingQueryOrContinuation = selectOrGroupClause.Parent.Parent;
if (containingQueryOrContinuation.IsKind(SyntaxKind.QueryExpression))
{
var containingQuery = (QueryExpressionSyntax)containingQueryOrContinuation;
containingBody = containingQuery.Body;
sourceIdentifier = containingQuery.FromClause.Identifier;
}
else
{
var containingContinuation = (QueryContinuationSyntax)containingQueryOrContinuation;
sourceIdentifier = containingContinuation.Identifier;
containingBody = containingContinuation.Body;
}
if (!SyntaxFactory.AreEquivalent(sourceIdentifier, selectorIdentifier))
{
return false;
}
if (selectOrGroupClause.IsKind(SyntaxKind.SelectClause) && containingBody.Clauses.Count == 0)
{
return false;
}
foreach (var clause in containingBody.Clauses)
{
if (!clause.IsKind(SyntaxKind.WhereClause) && !clause.IsKind(SyntaxKind.OrderByClause))
{
return false;
}
}
return true;
}