static ToStringRef TestForToStringAccess(ExpressionSyntax myItem) { var result = new ToStringRef { IsToString = false, HasArgument = false }; var invocation = myItem as InvocationExpressionSyntax; if (invocation != null) { var memberAccess = invocation.Expression as MemberAccessExpressionSyntax; if (memberAccess != null) { if (string.Equals("ToString", memberAccess.Name.ToString(), System.StringComparison.OrdinalIgnoreCase)) { // Only handle unecessary calls to ToString or calls with only a format string parameter. // IE: var.ToString() or var.ToString(""), but not var.ToString(a). if (!invocation.ArgumentList.Arguments.Any()) { result.IsToString = true; result.Expression = memberAccess.Expression; } else if (invocation.ArgumentList.Arguments.Count == 1) { var argument = invocation.ArgumentList.Arguments.First(); if (IsStringExpression(argument.Expression)) { result.IsToString = true; result.Expression = memberAccess.Expression; result.HasArgument = true; result.Argument = argument; } } } } } return(result); }
static ToStringRef TestForToStringAccess(ExpressionSyntax myItem) { var result = new ToStringRef { IsToString = false, HasArgument = false }; var invocation = myItem as InvocationExpressionSyntax; if (invocation != null) { var memberAccess = invocation.Expression as MemberAccessExpressionSyntax; if (memberAccess != null) { if (string.Equals("ToString", memberAccess.Name.ToString(), System.StringComparison.OrdinalIgnoreCase)) { // Only handle unecessary calls to ToString or calls with only a format string parameter. // IE: var.ToString() or var.ToString(""), but not var.ToString(a). if (!invocation.ArgumentList.Arguments.Any()) { result.IsToString = true; result.Expression = memberAccess.Expression; } else if (invocation.ArgumentList.Arguments.Count == 1) { var argument = invocation.ArgumentList.Arguments.First(); if (IsStringExpression(argument.Expression)) { result.IsToString = true; result.Expression = memberAccess.Expression; result.HasArgument = true; result.Argument = argument; } } } } } return result; }