Skip to content

Commit f6bced0

Browse files
committed
added configuration for generic param args
1 parent e3f65ad commit f6bced0

9 files changed

+69
-44
lines changed

ReadableExpressions/ExpressionExtensions.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
namespace AgileObjects.ReadableExpressions
22
{
3-
using System.Linq.Expressions;
3+
using System.Linq.Expressions;
44

5-
/// <summary>
6-
/// Provides the Expression translation extension method.
7-
/// </summary>
8-
public static class ExpressionExtensions
5+
/// <summary>
6+
/// Provides the Expression translation extension method.
7+
/// </summary>
8+
public static class ExpressionExtensions
99
{
1010
private static readonly ExpressionTranslatorRegistry _translatorRegistry = new ExpressionTranslatorRegistry();
1111

12-
/// <summary>
13-
/// Translates the given <paramref name="expression"/> to source-code string.
14-
/// </summary>
15-
/// <param name="expression">The Expression to translate.</param>
16-
/// <returns>The translated <paramref name="expression"/>.</returns>
17-
public static string ToReadableString(this Expression expression)
12+
/// <summary>
13+
/// Translates the given <paramref name="expression"/> to source-code string.
14+
/// </summary>
15+
/// <param name="expression">The Expression to translate.</param>
16+
/// <param name="settings">Configuration options for the translation</param>
17+
/// <returns>The translated <paramref name="expression"/>.</returns>
18+
public static string ToReadableString(this Expression expression, ReadableStringSettings settings = null)
1819
{
1920
return _translatorRegistry
20-
.Translate(expression)?
21+
.Translate(expression, settings ?? new ReadableStringSettings())?
2122
.WithoutUnindents();
2223
}
2324
}

ReadableExpressions/ExpressionTranslatorRegistry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public ExpressionTranslatorRegistry()
6161
.ToDictionary(t => t.NodeType, t => t.Translator);
6262
}
6363

64-
public string Translate(Expression expression)
64+
public string Translate(Expression expression, ReadableStringSettings settings)
6565
{
6666
var context = (expression != null)
67-
? TranslationContext.For(expression, Translate)
67+
? TranslationContext.For(expression, Translate, settings)
6868
: null;
6969

7070
return Translate(expression, context);
Binary file not shown.
Binary file not shown.

ReadableExpressions/ReadableExpressions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<RootNamespace>AgileObjects.ReadableExpressions</RootNamespace>
2727
<AssemblyVersion>1.8.6.0</AssemblyVersion>
2828
<FileVersion>1.8.6.0</FileVersion>
29-
<Version>1.8.6</Version>
29+
<Version>1.8.7</Version>
3030
</PropertyGroup>
3131

3232
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace AgileObjects.ReadableExpressions
2+
{
3+
/// <summary>
4+
/// Configuration for readable string generation
5+
/// </summary>
6+
public class ReadableStringSettings
7+
{
8+
/// <summary>
9+
/// Always specify generic parameter arguments explicitly in &lt;pointy,braces&gt;
10+
/// </summary>
11+
public bool AlwaysUseExplicitGenericParameters { get; set; }
12+
}
13+
}

ReadableExpressions/TranslationContext.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,30 @@ public class TranslationContext
1515
{
1616
private readonly ExpressionAnalysisVisitor _analyzer;
1717
private readonly Translator _globalTranslator;
18+
private readonly ReadableStringSettings _settings;
1819

19-
private TranslationContext(ExpressionAnalysisVisitor analyzer, Translator globalTranslator)
20+
private TranslationContext(ExpressionAnalysisVisitor analyzer, Translator globalTranslator, ReadableStringSettings settings)
2021
{
2122
_analyzer = analyzer;
2223
_globalTranslator = globalTranslator;
23-
}
24-
25-
/// <summary>
26-
/// Creates a <see cref="TranslationContext"/> containing information about the given
27-
/// <paramref name="expression"/>.
28-
/// </summary>
29-
/// <param name="expression">
30-
/// The <see cref="Expression"/> for which to create the <see cref="TranslationContext"/>.
31-
/// </param>
32-
/// <param name="globalTranslator">
33-
/// A global <see cref="Translator"/> delegate with which to perform translations.
34-
/// </param>
35-
/// <returns>A <see cref="TranslationContext"/> for the given<paramref name="expression"/>.</returns>
36-
public static TranslationContext For(Expression expression, Translator globalTranslator)
24+
_settings = settings;
25+
}
26+
27+
/// <summary>
28+
/// Creates a <see cref="TranslationContext"/> containing information about the given
29+
/// <paramref name="expression"/>.
30+
/// </summary>
31+
/// <param name="expression">
32+
/// The <see cref="Expression"/> for which to create the <see cref="TranslationContext"/>.
33+
/// </param>
34+
/// <param name="globalTranslator">
35+
/// A global <see cref="Translator"/> delegate with which to perform translations.
36+
/// </param>
37+
/// <param name="settings">Configuration for the translation</param>
38+
/// <returns>A <see cref="TranslationContext"/> for the given<paramref name="expression"/>.</returns>
39+
public static TranslationContext For(Expression expression, Translator globalTranslator, ReadableStringSettings settings)
3740
{
38-
return new TranslationContext(ExpressionAnalysisVisitor.Analyse(expression), globalTranslator);
41+
return new TranslationContext(ExpressionAnalysisVisitor.Analyse(expression), globalTranslator, settings);
3942
}
4043

4144
/// <summary>
@@ -44,12 +47,17 @@ public static TranslationContext For(Expression expression, Translator globalTra
4447
/// </summary>
4548
public IEnumerable<ParameterExpression> JoinedAssignmentVariables => _analyzer.JoinedAssignedVariables;
4649

47-
/// <summary>
48-
/// Translates the given <paramref name="expression"/> to readable source code.
49-
/// </summary>
50-
/// <param name="expression">The <see cref="Expression"/> to translate.</param>
51-
/// <returns>A source code translation of the given <paramref name="expression"/>.</returns>
52-
public string Translate(Expression expression)
50+
/// <summary>
51+
/// Configuration for translation in this context
52+
/// </summary>
53+
public ReadableStringSettings Settings { get => _settings; }
54+
55+
/// <summary>
56+
/// Translates the given <paramref name="expression"/> to readable source code.
57+
/// </summary>
58+
/// <param name="expression">The <see cref="Expression"/> to translate.</param>
59+
/// <returns>A source code translation of the given <paramref name="expression"/>.</returns>
60+
public string Translate(Expression expression)
5361
{
5462
return _globalTranslator.Invoke(expression, this);
5563
}

ReadableExpressions/Translators/MethodCallExpressionTranslator.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ internal string GetMethodCall(
120120
TranslationContext context)
121121
{
122122
var parametersString = context.TranslateParameters(arguments, method).WithParentheses();
123-
var genericArguments = GetGenericArgumentsIfNecessary(method);
123+
var genericArguments = GetGenericArgumentsIfNecessary(method, context.Settings.AlwaysUseExplicitGenericParameters);
124124

125125
return method.Name + genericArguments + parametersString;
126126
}
127127

128-
private static string GetGenericArgumentsIfNecessary(IMethodInfo method)
128+
private static string GetGenericArgumentsIfNecessary(IMethodInfo method, bool alwaysExplicit)
129129
{
130130
if (!method.IsGenericMethod)
131131
{
@@ -135,9 +135,12 @@ private static string GetGenericArgumentsIfNecessary(IMethodInfo method)
135135
var methodGenericDefinition = method.GetGenericMethodDefinition();
136136
var genericParameterTypes = methodGenericDefinition.GetGenericArguments().ToList();
137137

138-
RemoveSpecifiedGenericTypeParameters(
139-
methodGenericDefinition.GetParameters().Select(p => p.ParameterType),
140-
genericParameterTypes);
138+
if (!alwaysExplicit)
139+
{
140+
RemoveSpecifiedGenericTypeParameters(
141+
methodGenericDefinition.GetParameters().Select(p => p.ParameterType),
142+
genericParameterTypes);
143+
}
141144

142145
if (!genericParameterTypes.Any())
143146
{

VersionInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using System.Reflection;
22

3-
[assembly: AssemblyVersion("1.8.6")]
4-
[assembly: AssemblyFileVersion("1.8.6")]
3+
[assembly: AssemblyVersion("1.8.7")]
4+
[assembly: AssemblyFileVersion("1.8.7")]

0 commit comments

Comments
 (0)