本文整理汇总了C#中ExpressionSyntax.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.ToString方法的具体用法?C# ExpressionSyntax.ToString怎么用?C# ExpressionSyntax.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsResultDefinition
public static bool IsResultDefinition(ExpressionSyntax expr)
{
var methodRule = new Regex("^Contract.Result<.*?>\\(\\)$");
if (methodRule.IsMatch(expr.ToString()))
{
return true;
}
else if (expr is PrefixUnaryExpressionSyntax && expr.Kind == SyntaxKind.LogicalNotExpression)
{
return IsResultDefinition(((PrefixUnaryExpressionSyntax)expr).Operand);
}
if (expr is BinaryExpressionSyntax && expr.Kind == SyntaxKind.EqualsExpression)
{
var b = (BinaryExpressionSyntax)expr;
if (b.Left is InvocationExpressionSyntax)
{
var ie = (InvocationExpressionSyntax)b.Left;
var callRule = new Regex("^Contract.Result<.*?>");
return callRule.IsMatch(ie.Expression.ToString().Trim());
}
else
{
return false;
}
}
else
{
return false;
}
}
示例2: IsValueTypeAssigningToDefault
private static bool IsValueTypeAssigningToDefault(ITypeSymbol fieldType, ExpressionSyntax initializerValue, SemanticModel semanticModel)
{
switch (fieldType.SpecialType)
{
case SpecialType.System_Boolean:
{
var literal = initializerValue as LiteralExpressionSyntax;
if (literal == null) return false;
var boolValue = (bool)literal.Token.Value;
if (boolValue) return false;
break;
}
case SpecialType.System_SByte:
case SpecialType.System_Byte:
case SpecialType.System_Int16:
case SpecialType.System_UInt16:
case SpecialType.System_Int32:
case SpecialType.System_UInt32:
case SpecialType.System_Int64:
case SpecialType.System_UInt64:
case SpecialType.System_Decimal:
case SpecialType.System_Single:
case SpecialType.System_Double:
if (initializerValue.ToString() != "0")
{
var literal = initializerValue as LiteralExpressionSyntax;
if (literal == null) return false;
var possibleZero = Convert.ToDouble(literal.Token.Value);
if (possibleZero != 0) return false;
}
break;
case SpecialType.System_IntPtr:
{
var memberAccess = initializerValue as MemberAccessExpressionSyntax;
if (memberAccess == null) return false;
var memberAccessFieldSymbol = semanticModel.GetSymbolInfo(memberAccess).Symbol as IFieldSymbol;
if (memberAccessFieldSymbol?.ToString() != "System.IntPtr.Zero") return false;
break;
}
case SpecialType.System_UIntPtr:
{
var memberAccess = initializerValue as MemberAccessExpressionSyntax;
if (memberAccess == null) return false;
var memberAccessFieldSymbol = semanticModel.GetSymbolInfo(memberAccess).Symbol as IFieldSymbol;
if (memberAccessFieldSymbol?.ToString() != "System.UIntPtr.Zero") return false;
break;
}
case SpecialType.System_DateTime:
{
var memberAccess = initializerValue as MemberAccessExpressionSyntax;
if (memberAccess == null) return false;
var memberAccessFieldSymbol = semanticModel.GetSymbolInfo(memberAccess).Symbol as IFieldSymbol;
if (memberAccessFieldSymbol?.ToString() != "System.DateTime.MinValue") return false;
break;
}
//case SpecialType.System_Enum: //does not work, enums come back as none. Bug on roslyn? See solution below.
default:
if (fieldType.TypeKind != TypeKind.Enum) return false;
if (initializerValue.ToString() != "0")
{
var literal = initializerValue as LiteralExpressionSyntax;
if (literal == null) return false;
var possibleZero = Convert.ToDouble(literal.Token.Value);
if (possibleZero != 0) return false;
}
break;
}
return true;
}
示例3: CheckFollowingExpressions
private static void CheckFollowingExpressions(SyntaxNodeAnalysisContext context, int currentExpressionIndex,
List<ExpressionSyntax> expressionsInChain,
ExpressionSyntax expressionComparedToNull, BinaryExpressionSyntax comparisonToNull)
{
for (var j = currentExpressionIndex + 1; j < expressionsInChain.Count; j++)
{
var descendantNodes = expressionsInChain[j].DescendantNodes()
.Where(descendant =>
descendant.IsKind(expressionComparedToNull.Kind()) &&
EquivalenceChecker.AreEquivalent(expressionComparedToNull, descendant))
.Where(descendant =>
(descendant.Parent is MemberAccessExpressionSyntax &&
EquivalenceChecker.AreEquivalent(expressionComparedToNull,
((MemberAccessExpressionSyntax) descendant.Parent).Expression)) ||
(descendant.Parent is ElementAccessExpressionSyntax &&
EquivalenceChecker.AreEquivalent(expressionComparedToNull,
((ElementAccessExpressionSyntax) descendant.Parent).Expression)))
.ToList();
if (descendantNodes.Any())
{
context.ReportDiagnostic(Diagnostic.Create(Rule, comparisonToNull.GetLocation(),
expressionComparedToNull.ToString()));
}
}
}
示例4: CheckWhetherMethodIsFromNamespace
private bool CheckWhetherMethodIsFromNamespace(ExpressionSyntax node)
{
var isMatch = false;
if (SemanticModel != null)
{
var symbolInfo = SemanticModel.GetSymbolInfo(node);
string ns = symbolInfo.Symbol.ContainingNamespace.ToDisplayString();
if (ns == Namespace)
{
Results.AppendLine();
Results.Append("Line ");
Results.Append(SemanticModel.SyntaxTree.GetLineSpan(node.Span).StartLinePosition.Line);
Results.Append(": ");
Results.Append(node.ToString());
isMatch = true;
}
}
return isMatch;
}
示例5: ReportDiagnostic
private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, VariableDeclaratorSyntax variable, ExpressionSyntax initializerValue)
{
var diag = Diagnostic.Create(Rule, variable.GetLocation(), variable.Identifier.ValueText, initializerValue.ToString());
context.ReportDiagnostic(diag);
}
示例6: RewritePostfixUnarys
protected ExpressionSyntax RewritePostfixUnarys(ExpressionSyntax node)
{
var expressions = node.DescendentNodesAndSelf().OfType<PostfixUnaryExpressionSyntax>().ToArray();
if (expressions.Length == 0)
return node;
List<string> names = new List<string> (expressions.Length);
foreach (var expr in expressions)
{
if (expr.Kind != SyntaxKind.PostIncrementExpression && expr.Kind != SyntaxKind.PostDecrementExpression)
continue; // These aren't the droids we're looking for.
IdentifierNameSyntax name = FindIdentifierName (expr);
if (name == null)
continue; // We didn't find a name
if (names.Contains (name.PlainName))
{
var newExpr = GetLogExpression (name.PlainName, expr);
node = node.ReplaceNode (expr, newExpr);
}
else
names.Add (name.PlainName);
}
// We'll use a string builder construct our expression.
StringBuilder builder = new StringBuilder ("LogPostfixValues ((");
builder.Append (node.ToString());
builder.Append ("), ");
bool found = false;
foreach (string name in names)
{
if (found)
builder.Append (", ");
// We need to specify our generic types so we match
// the params argument type
builder.Append ("new Tuple<string, object> (\"");
builder.Append (name);
builder.Append ("\", ");
builder.Append (name);
builder.Append (")");
found = true;
}
if (!found)
return node; // We didn't find any usable expressions
builder.Append (")");
// Parse and return our new expression.
return Syntax.ParseExpression (builder.ToString());
}
示例7: Expression
public static string Expression(ExpressionSyntax expression)
{
return expression.ToString();
}
示例8: AddWatchNode
SyntaxNode AddWatchNode(StatementSyntax node, ExpressionSyntax expr)
{
var id = Guid.NewGuid().ToString();
var c = node.GetTrailingTrivia().First(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia && t.ToString().StartsWith("//="));
var p = c.GetLocation().GetLineSpan().StartLinePosition;
var wv = new WatchVariable {
Id = id,
Expression = expr.ToString(),
ExplicitExpression = "",
FilePath = path,
FileLine = p.Line + 1, // 0-based index
FileColumn = p.Character + 1, // 0-based index
};
WatchVariables.Add(wv);
var wi = GetWatchInstrument(id, expr);
// creating a block and removing the open/close braces is a bit of a hack but
// lets us replace one node with two...
return
SyntaxFactory
.Block(node, wi)
.WithOpenBraceToken(SyntaxFactory.MissingToken(SyntaxKind.OpenBraceToken))
.WithCloseBraceToken(SyntaxFactory.MissingToken(SyntaxKind.CloseBraceToken))
.WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n"));
}
示例9: assigmentStatement
private static StatementSyntax assigmentStatement(ExpressionSyntax left, ExpressionSyntax right, Scope scope)
{
if (left is IdentifierNameSyntax)
{
var varName = left.ToString();
if (scope.hasVariable(varName))
return assignment.Get<StatementSyntax>(left, right);
else
{
scope.addVariable(varName);
return declaration.Get<StatementSyntax>(varName, right);
}
}
throw new NotImplementedException();
}
示例10: ShouldReplaceStringLength
/// <summary>
/// Determines whether a binary expression with a string length expression is suitable for replacement.
/// </summary>
/// <param name="left">A node representing a string length expression.</param>
/// <param name="right">A node to be tested.</param>
/// <param name="operatorToken">The operator separating the nodes.</param>
/// <returns></returns>
static ShouldReplaceResult ShouldReplaceStringLength(MemberAccessExpressionSyntax left, ExpressionSyntax right, SyntaxToken operatorToken)
{
const string zeroLiteral = "0";
const string oneLiteral = "1";
var result = new ShouldReplaceResult();
result.ShouldReplace = false;
// str.Length == 0 or str.Length <= 0
if (operatorToken.IsKind(SyntaxKind.EqualsEqualsToken, SyntaxKind.LessThanEqualsToken) && string.Equals(zeroLiteral, right.ToString()))
{
result.IsEmptyTest = true;
result.ShouldReplace = true;
}
// str.Length < 1
else if (operatorToken.IsKind(SyntaxKind.LessThanToken) && string.Equals(oneLiteral, right.ToString()))
{
result.IsEmptyTest = true;
result.ShouldReplace = true;
}
// str.Length != 0 or str.Length > 0
else if (operatorToken.IsKind(SyntaxKind.ExclamationEqualsToken, SyntaxKind.GreaterThanToken) && string.Equals(zeroLiteral, right.ToString()))
{
result.IsEmptyTest = true;
result.IsNegative = true;
result.ShouldReplace = true;
}
// str.Length >= 1
else if (operatorToken.IsKind(SyntaxKind.GreaterThanEqualsToken) && string.Equals(oneLiteral, right.ToString()))
{
result.IsEmptyTest = true;
result.IsNegative = true;
result.ShouldReplace = true;
}
if (result.ShouldReplace)
{
result.IdentifierNode = left.Expression;
}
return result;
}
示例11: IsEmptySyntax
/// <summary>
/// Does the expression look like a test for empty string ("" or string.Empty)?
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
static bool IsEmptySyntax(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax node)
{
if (!IsStringType(nodeContext, node))
return false;
if (node.IsKind(SyntaxKind.StringLiteralExpression))
{
if (string.Equals("\"\"", node.ToString()))
return true;
}
else if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
var sma = node as MemberAccessExpressionSyntax;
if (!string.Equals("string", sma.Expression.ToString(), StringComparison.OrdinalIgnoreCase))
return false;
if (!string.Equals("Empty", sma.Name.ToString(), StringComparison.OrdinalIgnoreCase))
return false;
return true;
}
return false;
}
示例12: ConstantType
public static TypeSyntax ConstantType(ExpressionSyntax value)
{
var valueStr = value.ToString();
switch (value.CSharpKind())
{
case SyntaxKind.NumericLiteralExpression:
{
int val;
double dval;
if (int.TryParse(valueStr, out val))
return Compiler.Int;
else if (double.TryParse(valueStr, out dval))
return Compiler.Double;
break;
}
case SyntaxKind.StringLiteralExpression:
return Compiler.String;
case SyntaxKind.TrueLiteralExpression:
case SyntaxKind.FalseLiteralExpression:
return Compiler.Boolean;
}
return SyntaxFactory.ParseTypeName(valueStr);
}
示例13: ConditionContainsNullCheck
bool ConditionContainsNullCheck(ExpressionSyntax condition, ExpressionSyntax identifierToCheck)
{
var identifierNameToCheck = identifierToCheck as IdentifierNameSyntax;
var memberAccessExpressionToCheck = identifierToCheck as MemberAccessExpressionSyntax;
return condition.DescendantNodesAndSelf().Any(n =>
{
var binaryExpr = n as BinaryExpressionSyntax;
if (binaryExpr != null)
{
IdentifierNameSyntax identifierName = binaryExpr.Left as IdentifierNameSyntax;
if ((identifierName != null) && (identifierNameToCheck != null) && (identifierName.Identifier.ValueText == identifierNameToCheck.Identifier.ValueText))
return binaryExpr.IsKind(SyntaxKind.IsNotExpression) && binaryExpr.Right.IsKind(SyntaxKind.NothingLiteralExpression);
MemberAccessExpressionSyntax memberAccessExpressionSyntax = binaryExpr.Left as MemberAccessExpressionSyntax;
if ((memberAccessExpressionSyntax != null) && (memberAccessExpressionToCheck != null) && (memberAccessExpressionSyntax.ToString() == identifierToCheck.ToString()))
return binaryExpr.IsKind(SyntaxKind.IsNotExpression) && binaryExpr.Right.IsKind(SyntaxKind.NothingLiteralExpression);
identifierName = binaryExpr.Right as IdentifierNameSyntax;
if ((identifierName != null) && (identifierNameToCheck != null) && (identifierName.Identifier.ValueText == identifierNameToCheck.Identifier.ValueText))
return binaryExpr.IsKind(SyntaxKind.IsNotExpression) && binaryExpr.Left.IsKind(SyntaxKind.NothingLiteralExpression);
memberAccessExpressionSyntax = binaryExpr.Right as MemberAccessExpressionSyntax;
if ((memberAccessExpressionSyntax != null) && (memberAccessExpressionToCheck != null) && (memberAccessExpressionSyntax.ToString() == identifierToCheck.ToString()))
return binaryExpr.IsKind(SyntaxKind.IsNotExpression) && binaryExpr.Left.IsKind(SyntaxKind.NothingLiteralExpression);
}
return false;
});
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:29,代码来源:AddCheckForNothingCodeRefactoringProvider.cs
示例14: CheckForBooleanConstant
private static void CheckForBooleanConstant(BinaryExpressionSyntax binaryExpression, bool leftSide,
ExpressionSyntax booleanContantExpression, bool needsLocationCalculation, SyntaxNodeAnalysisContext c)
{
var expression = leftSide
? binaryExpression.Left
: binaryExpression.Right;
if (EquivalenceChecker.AreEquivalent(expression, booleanContantExpression))
{
var location = needsLocationCalculation
? CalculateLocation(binaryExpression, leftSide)
: expression.GetLocation();
c.ReportDiagnostic(Diagnostic.Create(Rule, location,
booleanContantExpression.ToString()));
}
}
示例15: ConvertScopedIdentifierName
public static string ConvertScopedIdentifierName(ExpressionSyntax scope, string name)
{
if (scope == null)
return ConvertIdentifierName(name);
switch (name)
{
case "out":
if (scope.ToString() == "System")
return "Console";
goto default;
case "MAX_VALUE":
case "MIN_VALUE":
switch (scope.ToString())
{
case "int":
case "long":
return Humanize(name);
}
goto default;
default:
return ConvertIdentifierName(name);
}
}