Skip to content

Commit f0fdbba

Browse files
committed
Expression refactoring from master
1 parent 2fa7a56 commit f0fdbba

12 files changed

+379
-61
lines changed

Main/src/CodeJam.Main.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
<Compile Include="Collections\QueryableExtensions.ApplyOrder.cs" />
107107
<Compile Include="Collections\QueryableExtensions.cs" />
108108
<Compile Include="Expressions\ExpressionExtensions.cs" />
109+
<Compile Include="Expressions\ExpressionHelper.Func.generated.cs">
110+
<AutoGen>True</AutoGen>
111+
<DesignTime>True</DesignTime>
112+
<DependentUpon>ExpressionHelper.Func.tt</DependentUpon>
113+
</Compile>
109114
<Compile Include="IO\NamespaceDoc.cs" />
110115
<Compile Include="NamespaceDoc.cs" />
111116
<Compile Include="Reflection\NamespaceDoc.cs" />
@@ -184,7 +189,7 @@
184189
<Compile Include="Structures\Option`1.cs" />
185190
<Compile Include="Properties\AssemblyInfo.cs" />
186191
<Compile Include="Reflection\EnumHelper.cs" />
187-
<Compile Include="Reflection\ExpressionHelper.cs" />
192+
<Compile Include="Expressions\ExpressionExtensions.GetMembers.cs" />
188193
<Compile Include="Reflection\InfoOf.cs" />
189194
<Compile Include="Reflection\ReflectionExtensions.cs" />
190195
<Compile Include="Strings\CharExtensions.cs" />
@@ -236,6 +241,10 @@
236241
<Generator>TextTemplatingFileGenerator</Generator>
237242
<LastGenOutput>Operators.Numeric.generated.cs</LastGenOutput>
238243
</Content>
244+
<Content Include="Expressions\ExpressionHelper.Func.tt">
245+
<Generator>TextTemplatingFileGenerator</Generator>
246+
<LastGenOutput>ExpressionHelper.Func.generated.cs</LastGenOutput>
247+
</Content>
239248
<Content Include="Strings\CharExtensions.Infix.tt">
240249
<Generator>TextTemplatingFileGenerator</Generator>
241250
<LastGenOutput>CharExtensions.Infix.generated.cs</LastGenOutput>

Main/src/Reflection/ExpressionHelper.cs renamed to Main/src/Expressions/ExpressionExtensions.GetMembers.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
using JetBrains.Annotations;
66

7-
namespace CodeJam.Reflection
7+
namespace CodeJam.Expressions
88
{
9-
/// <summary>
10-
/// Provides a helper class to get the property, field, ctor or method from an expression.
11-
/// </summary>
12-
[PublicAPI]
13-
public static class ExpressionHelper
9+
public static partial class ExpressionExtensions
1410
{
1511
/// <summary>
1612
/// Gets the <see cref="MemberInfo"/>.
@@ -20,8 +16,10 @@ public static class ExpressionHelper
2016
/// The <see cref="MemberInfo"/> instance.
2117
/// </returns>
2218
[NotNull, Pure]
23-
public static MemberInfo GetMemberInfo([NotNull] LambdaExpression expression)
19+
public static MemberInfo GetMemberInfo([NotNull] this LambdaExpression expression)
2420
{
21+
Code.NotNull(expression, nameof(expression));
22+
2523
var body = expression.Body;
2624
var unary = body as UnaryExpression;
2725
if (unary != null)
@@ -46,7 +44,7 @@ public static MemberInfo GetMemberInfo([NotNull] LambdaExpression expression)
4644
/// The <see cref="PropertyInfo"/> instance.
4745
/// </returns>
4846
[NotNull, Pure]
49-
public static PropertyInfo GetProperty([NotNull] LambdaExpression expression) =>
47+
public static PropertyInfo GetProperty([NotNull] this LambdaExpression expression) =>
5048
(PropertyInfo)GetMemberExpression(expression).Member;
5149

5250
/// <summary>
@@ -57,7 +55,7 @@ public static PropertyInfo GetProperty([NotNull] LambdaExpression expression) =>
5755
/// The <see cref="FieldInfo"/> instance.
5856
/// </returns>
5957
[NotNull, Pure]
60-
public static FieldInfo GetField([NotNull] LambdaExpression expression) =>
58+
public static FieldInfo GetField([NotNull] this LambdaExpression expression) =>
6159
(FieldInfo)GetMemberExpression(expression).Member;
6260

6361
/// <summary>
@@ -68,7 +66,7 @@ public static FieldInfo GetField([NotNull] LambdaExpression expression) =>
6866
/// The <see cref="ConstructorInfo"/> instance.
6967
/// </returns>
7068
[NotNull, Pure]
71-
public static ConstructorInfo GetConstructor([NotNull] LambdaExpression expression) =>
69+
public static ConstructorInfo GetConstructor([NotNull] this LambdaExpression expression) =>
7270
(ConstructorInfo)GetMemberInfo(expression);
7371

7472
/// <summary>
@@ -79,7 +77,7 @@ public static ConstructorInfo GetConstructor([NotNull] LambdaExpression expressi
7977
/// The <see cref="MethodInfo"/> instance.
8078
/// </returns>
8179
[NotNull, Pure]
82-
public static MethodInfo GetMethod([NotNull] LambdaExpression expression)
80+
public static MethodInfo GetMethod([NotNull] this LambdaExpression expression)
8381
{
8482
var info = GetMemberInfo(expression);
8583
return
@@ -96,7 +94,7 @@ info is PropertyInfo
9694
/// A name of the property.
9795
/// </returns>
9896
[NotNull, Pure]
99-
public static string GetPropertyName([NotNull] LambdaExpression expression) =>
97+
public static string GetPropertyName([NotNull] this LambdaExpression expression) =>
10098
GetMemberExpression(expression).Member.Name;
10199

102100
/// <summary>
@@ -107,7 +105,7 @@ public static string GetPropertyName([NotNull] LambdaExpression expression) =>
107105
/// A composited name of the property.
108106
/// </returns>
109107
[NotNull, Pure]
110-
public static string GetFullPropertyName([NotNull] LambdaExpression expression) =>
108+
public static string GetFullPropertyName([NotNull] this LambdaExpression expression) =>
111109
GetFullPropertyNameImpl(GetMemberExpression(expression));
112110

113111
/// <summary>
@@ -118,7 +116,7 @@ public static string GetFullPropertyName([NotNull] LambdaExpression expression)
118116
/// A name of the method.
119117
/// </returns>
120118
[NotNull, Pure]
121-
public static string GetMethodName([NotNull] LambdaExpression expression) =>
119+
public static string GetMethodName([NotNull] this LambdaExpression expression) =>
122120
GetMethod(expression).Name;
123121

124122
private static string GetFullPropertyNameImpl(MemberExpression expression)

Main/src/Expressions/ExpressionExtensions.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Linq.Expressions;
56

@@ -11,17 +12,16 @@ namespace CodeJam.Expressions
1112
/// <see cref="Expression"/> Extensions.
1213
/// </summary>
1314
[PublicAPI]
14-
public static class ExpressionExtensions
15+
public static partial class ExpressionExtensions
1516
{
1617
#region VisitInternal
17-
18-
static void VisitInternal<T>(IEnumerable<T> source, Action<T> func)
18+
private static void VisitInternal<T>(IEnumerable<T> source, Action<T> func)
1919
{
2020
foreach (var item in source)
2121
func(item);
2222
}
2323

24-
static void VisitInternal<T>(IEnumerable<T> source, Action<Expression> func)
24+
private static void VisitInternal<T>(IEnumerable<T> source, Action<Expression> func)
2525
where T : Expression
2626
{
2727
foreach (var item in source)
@@ -40,7 +40,7 @@ public static void Visit([CanBeNull] this Expression expr, [NotNull] Action<Expr
4040
VisitInternal(expr, func);
4141
}
4242

43-
static void VisitInternal(this Expression expr, Action<Expression> func)
43+
private static void VisitInternal(this Expression expr, Action<Expression> func)
4444
{
4545
if (expr == null)
4646
return;
@@ -303,13 +303,13 @@ static void VisitInternal(this Expression expr, Action<Expression> func)
303303
func(expr);
304304
}
305305

306-
static void VisitInternal<T>(IEnumerable<T> source, Func<T,bool> func)
306+
private static void VisitInternal<T>(IEnumerable<T> source, Func<T,bool> func)
307307
{
308308
foreach (var item in source)
309309
func(item);
310310
}
311311

312-
static void VisitInternal<T>(IEnumerable<T> source, Func<Expression,bool> func)
312+
private static void VisitInternal<T>(IEnumerable<T> source, Func<Expression,bool> func)
313313
where T : Expression
314314
{
315315
foreach (var item in source)
@@ -328,7 +328,8 @@ public static void Visit([CanBeNull] this Expression expr, [NotNull] Func<Expres
328328
VisitInternal(expr, func);
329329
}
330330

331-
static void VisitInternal(this Expression expr, Func<Expression,bool> func)
331+
[SuppressMessage("ReSharper", "TailRecursiveCall")]
332+
private static void VisitInternal(this Expression expr, Func<Expression,bool> func)
332333
{
333334
if (expr == null || !func(expr))
334335
return;
@@ -590,8 +591,7 @@ static void VisitInternal(this Expression expr, Func<Expression,bool> func)
590591
#endregion
591592

592593
#region Find
593-
594-
static Expression FindInternal<T>(IEnumerable<T> source, Func<T,Expression> func)
594+
private static Expression FindInternal<T>(IEnumerable<T> source, Func<T,Expression> func)
595595
{
596596
foreach (var item in source)
597597
{
@@ -603,7 +603,7 @@ static Expression FindInternal<T>(IEnumerable<T> source, Func<T,Expression> func
603603
return null;
604604
}
605605

606-
static Expression FindInternal<T>(IEnumerable<T> source, Func<Expression,bool> func)
606+
private static Expression FindInternal<T>(IEnumerable<T> source, Func<Expression,bool> func)
607607
where T : Expression
608608
{
609609
foreach (var item in source)
@@ -642,7 +642,8 @@ public static Expression Find([CanBeNull] this Expression expr, [NotNull] Func<E
642642
return FindInternal(expr, func);
643643
}
644644

645-
static Expression FindInternal(this Expression expr, Func<Expression,bool> func)
645+
[SuppressMessage("ReSharper", "TailRecursiveCall")]
646+
private static Expression FindInternal(this Expression expr, Func<Expression,bool> func)
646647
{
647648
if (expr == null || func(expr))
648649
return expr;
@@ -932,7 +933,7 @@ public static Expression ReplaceParameters(
932933
});
933934
}
934935

935-
static IEnumerable<T> TransformInternal<T>(ICollection<T> source, Func<T,T> func)
936+
private static IEnumerable<T> TransformInternal<T>(ICollection<T> source, Func<T,T> func)
936937
where T : class
937938
{
938939
var modified = false;
@@ -948,7 +949,7 @@ static IEnumerable<T> TransformInternal<T>(ICollection<T> source, Func<T,T> func
948949
return modified ? list : source;
949950
}
950951

951-
static IEnumerable<T> TransformInternal<T>(ICollection<T> source, Func<Expression,Expression> func)
952+
private static IEnumerable<T> TransformInternal<T>(ICollection<T> source, Func<Expression,Expression> func)
952953
where T : Expression
953954
{
954955
var modified = false;
@@ -993,7 +994,7 @@ public static Expression Transform([CanBeNull] this Expression expr, [NotNull] F
993994
return TransformInternal(expr, func);
994995
}
995996

996-
static Expression TransformInternal(this Expression expr, Func<Expression,Expression> func)
997+
private static Expression TransformInternal(this Expression expr, Func<Expression,Expression> func)
997998
{
998999
if (expr == null)
9991000
return null;
@@ -1109,7 +1110,7 @@ static Expression TransformInternal(this Expression expr, Func<Expression,Expres
11091110
var b = TransformInternal(e.Body, func);
11101111
var p = TransformInternal(e.Parameters, func);
11111112

1112-
return b != e.Body || p != e.Parameters ? Expression.Lambda(expr.Type, b, p.ToArray()) : expr;
1113+
return b != e.Body || !ReferenceEquals(p, e.Parameters) ? Expression.Lambda(expr.Type, b, p.ToArray()) : expr;
11131114
}
11141115

11151116
case ExpressionType.ListInit:
@@ -1120,7 +1121,7 @@ static Expression TransformInternal(this Expression expr, Func<Expression,Expres
11201121
TransformInternal(e.Initializers, p =>
11211122
{
11221123
var args = TransformInternal(p.Arguments, func);
1123-
return args != p.Arguments? Expression.ElementInit(p.AddMethod, args): p;
1124+
return !ReferenceEquals(args, p.Arguments)? Expression.ElementInit(p.AddMethod, args): p;
11241125
}));
11251126
}
11261127

@@ -1149,7 +1150,7 @@ static Expression TransformInternal(this Expression expr, Func<Expression,Expres
11491150
TransformInternal(ml.Initializers, p =>
11501151
{
11511152
var args = TransformInternal(p.Arguments, func);
1152-
return args != p.Arguments? Expression.ElementInit(p.AddMethod, args): p;
1153+
return !ReferenceEquals(args, p.Arguments)? Expression.ElementInit(p.AddMethod, args): p;
11531154
}));
11541155
}
11551156

0 commit comments

Comments
 (0)