Skip to content

Commit

Permalink
Reduce lambda usage
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Nov 3, 2017
1 parent c864081 commit fa331cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
12 changes: 2 additions & 10 deletions Source/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,9 @@ private static bool ReturnsMatch(MethodCallExpression expression)
/// TODO: remove this code when https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=331583
/// is fixed.
/// </devdoc>
public static string ToStringFixed(this Expression expression)
public static string ToStringFixed(this Expression expression, bool useFullTypeName = false)
{
return ExpressionStringBuilder.GetString(expression);
}

internal static string ToStringFixed(this Expression expression, bool useFullTypeName)
{
if (useFullTypeName)
return ExpressionStringBuilder.GetString(expression, type => type.FullName);
else
return ExpressionStringBuilder.GetString(expression, type => type.Name);
return ExpressionStringBuilder.GetString(expression, useFullTypeName);
}

/// <summary>
Expand Down
52 changes: 30 additions & 22 deletions Source/ExpressionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,18 @@ internal class ExpressionStringBuilder
{
private readonly Expression expression;
private StringBuilder builder;
private Func<Type, string> getTypeName;
private bool useFullName;

internal static string GetString(Expression expression)
internal static string GetString(Expression expression, bool useFullName = false)
{
return GetString(expression, type => type.Name);
}

internal static string GetString(Expression expression, Func<Type, string> getTypeName)
{
var builder = new ExpressionStringBuilder(getTypeName, expression);
var builder = new ExpressionStringBuilder(expression, useFullName);
return builder.ToString();
}

public ExpressionStringBuilder(Func<Type, string> getTypeName, Expression expression)
public ExpressionStringBuilder(Expression expression, bool useFullName)
{
this.getTypeName = getTypeName;
this.expression = expression;
this.useFullName = useFullName;
}

public override string ToString()
Expand Down Expand Up @@ -165,7 +160,7 @@ private void ToStringUnary(UnaryExpression u)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
builder.Append('(').Append(this.getTypeName(u.Type)).Append(')');
builder.Append('(').Append(this.useFullName ? u.Type.FullName : u.Type.Name).Append(')');
ToString(u.Operand);
return;

Expand Down Expand Up @@ -194,7 +189,7 @@ private void ToStringUnary(UnaryExpression u)
builder.Append('(');
ToString(u.Operand);
builder.Append(" as ");
builder.AppendDisplayName(u.Type, this.getTypeName);
builder.AppendDisplayName(u.Type, this.useFullName);
builder.Append(')');
return;
}
Expand Down Expand Up @@ -292,7 +287,7 @@ private void ToStringConstant(ConstantExpression c)
}
else if (c.Type.GetTypeInfo().IsEnum)
{
builder.AppendDisplayName(c.Type, this.getTypeName).Append('.').Append(value);
builder.AppendDisplayName(c.Type, this.useFullName).Append('.').Append(value);
}
else
{
Expand Down Expand Up @@ -333,7 +328,7 @@ private void ToStringMemberAccess(MemberExpression m)
}
else
{
builder.AppendDisplayName(m.Member.DeclaringType, this.getTypeName);
builder.AppendDisplayName(m.Member.DeclaringType, this.useFullName);
}
builder.Append('.');
builder.Append(m.Member.Name);
Expand Down Expand Up @@ -361,7 +356,8 @@ private void ToStringMethodCall(MethodCallExpression node)
}
else // Method is static
{
this.builder.Append(this.getTypeName(node.Method.DeclaringType));
var nodeMethodDeclaringType = node.Method.DeclaringType;
this.builder.Append(this.useFullName ? nodeMethodDeclaringType.FullName : nodeMethodDeclaringType.Name);
}

if (node.Method.IsPropertyIndexerGetter())
Expand Down Expand Up @@ -397,8 +393,20 @@ private void ToStringMethodCall(MethodCallExpression node)
this.builder
.Append('.')
.Append(node.Method.Name)
.Append('<')
.Append(string.Join(",", node.Method.GetGenericArguments().Select(s => this.getTypeName(s)).ToArray()))
.Append('<');

var nodeMethodGenericArguments = node.Method.GetGenericArguments();
for (int i = 0, n = nodeMethodGenericArguments.Length; i < n; ++i)
{
if (i > 0)
{
this.builder.Append(", ");
}
var name =
this.builder.Append(this.useFullName ? nodeMethodGenericArguments[i].FullName : nodeMethodGenericArguments[i].Name);
}

this.builder
.Append(">(");
AsCommaSeparatedValues(node.Arguments.Skip(paramFrom), ToString);
this.builder.Append(')');
Expand Down Expand Up @@ -486,7 +494,7 @@ private void ToStringNew(NewExpression nex)
{
Type type = (nex.Constructor == null) ? nex.Type : nex.Constructor.DeclaringType;
builder.Append("new ");
builder.AppendDisplayName(type, this.getTypeName);
builder.AppendDisplayName(type, this.useFullName);
builder.Append('(');
AsCommaSeparatedValues(nex.Arguments, ToString);
builder.Append(')');
Expand Down Expand Up @@ -531,7 +539,7 @@ private void ToStringNewArray(NewArrayExpression na)
return;
case ExpressionType.NewArrayBounds:
builder.Append("new ");
builder.AppendDisplayName(na.Type.GetElementType(), this.getTypeName);
builder.AppendDisplayName(na.Type.GetElementType(), this.useFullName);
builder.Append('[');
AsCommaSeparatedValues(na.Expressions, ToString);
builder.Append(']');
Expand Down Expand Up @@ -632,11 +640,11 @@ internal static string ToStringOperator(ExpressionType nodeType)

internal static class StringBuilderExtensions
{
public static StringBuilder AppendDisplayName(this StringBuilder builder, Type source, Func<Type, string> getName)
public static StringBuilder AppendDisplayName(this StringBuilder builder, Type source, bool useFullName)
{
Debug.Assert(source != null);

var name = getName(source);
var name = useFullName ? source.FullName : source.Name;
var backtickIndex = name.IndexOf('`');
if (backtickIndex >= 0)
{
Expand All @@ -657,7 +665,7 @@ public static StringBuilder AppendDisplayName(this StringBuilder builder, Type s
{
builder.Append(", ");
}
builder.Append(getName(genericArguments[i]));
builder.Append(useFullName ? genericArguments[i].FullName : genericArguments[i].Name);
}
builder.Append('>');
}
Expand Down

0 comments on commit fa331cb

Please sign in to comment.