-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ca217e
commit db5749b
Showing
2 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
|
||
namespace DNX.Extensions.Reflection | ||
{ | ||
/// <summary> | ||
/// Class ExpressionExtensions. | ||
/// </summary> | ||
public static class ExpressionExtensions | ||
{ | ||
/// <summary> | ||
/// Determines whether the func is a member expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The func.</param> | ||
/// <returns></returns> | ||
public static bool IsMemberExpression<T>(this Expression<Func<T>> func) | ||
{ | ||
return func.Body is MemberExpression; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the action is a member expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="action">The action.</param> | ||
/// <returns></returns> | ||
public static bool IsMemberExpression<T>(this Expression<Action> action) | ||
{ | ||
return action.Body is MemberExpression; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the func is a lambda expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The func.</param> | ||
/// <returns></returns> | ||
public static bool IsLambdaExpression<T>(Expression<Func<T>> func) | ||
{ | ||
return func.Body is LambdaExpression; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the action is a lambda expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="action">The action.</param> | ||
/// <returns></returns> | ||
public static bool IsLambdaExpression<T>(Expression<Action<T>> action) | ||
{ | ||
return action.Body is LambdaExpression; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the func is a unary expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The func.</param> | ||
/// <returns></returns> | ||
public static bool IsUnaryExpression<T>(Expression<Func<T>> func) | ||
{ | ||
var unaryExpression = func.Body as UnaryExpression; | ||
|
||
return unaryExpression != null; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the action is a unary expression | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="action">The action.</param> | ||
/// <returns></returns> | ||
public static bool IsUnaryExpression<T>(Expression<Action<T>> action) | ||
{ | ||
var unaryExpression = action.Body as UnaryExpression; | ||
|
||
return unaryExpression != null; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the member expression. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The func.</param> | ||
/// <returns></returns> | ||
public static string GetMemberName<T>(Expression<Func<T>> func) | ||
{ | ||
var memberExpression = func.Body as MemberExpression; | ||
|
||
return memberExpression?.Member.Name; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the lambda expression. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="exp">The exp.</param> | ||
/// <returns></returns> | ||
public static string GetLambdaName<T>(Expression<Func<T>> exp) | ||
{ | ||
var lambdaExpression = exp.Body as LambdaExpression; | ||
|
||
return lambdaExpression?.Name; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the unary expression. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="exp">The exp.</param> | ||
/// <returns></returns> | ||
public static string GetUnaryName<T>(Expression<Func<T>> exp) | ||
{ | ||
var unaryExpression = exp.Body as UnaryExpression; //((UnaryExpression)exp.Body).Operand; | ||
|
||
var operand = unaryExpression?.Operand as MemberExpression; | ||
|
||
return operand?.Member.Name; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the expression. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="exp">The exp.</param> | ||
/// <returns></returns> | ||
public static string GetExpressionName<T>(Expression<Func<T>> exp) | ||
{ | ||
if (IsMemberExpression(exp)) | ||
{ | ||
return GetMemberName(exp); | ||
} | ||
|
||
if (IsLambdaExpression(exp)) | ||
{ | ||
return GetLambdaName(exp); | ||
} | ||
|
||
if (IsUnaryExpression(exp)) | ||
{ | ||
return GetUnaryName(exp); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/DNX.Extensions.Tests/Reflection/ExpressionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using DNX.Extensions.Reflection; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
#pragma warning disable xUnit1044 // TestData not serializable | ||
|
||
namespace DNX.Extensions.Tests.Reflection; | ||
|
||
public class ExpressionExtensionsTests | ||
{ | ||
// TODO: Not sure how to test these | ||
|
||
|
||
|
||
|
||
[Fact] | ||
public void IsMemberExpression_func_Tests() | ||
{ | ||
ExpressionExtensions.IsMemberExpression(() => BooleanFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsMemberExpression(() => StringFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsMemberExpression(() => Int32Func()).Should().BeFalse(); | ||
ExpressionExtensions.IsMemberExpression(() => DoubleFunc()).Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IsMemberExpression_action_Tests() | ||
{ | ||
// TODO: This looks wrong | ||
ExpressionExtensions.IsMemberExpression<object>(() => Action1()).Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IsLambdaExpression_func_Tests() | ||
{ | ||
ExpressionExtensions.IsLambdaExpression(() => BooleanFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsLambdaExpression(() => StringFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsLambdaExpression(() => Int32Func()).Should().BeFalse(); | ||
ExpressionExtensions.IsLambdaExpression(() => DoubleFunc()).Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IsUnaryExpression_func_Tests() | ||
{ | ||
ExpressionExtensions.IsUnaryExpression(() => BooleanFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsUnaryExpression(() => StringFunc()).Should().BeFalse(); | ||
ExpressionExtensions.IsUnaryExpression(() => Int32Func()).Should().BeFalse(); | ||
ExpressionExtensions.IsUnaryExpression(() => DoubleFunc()).Should().BeFalse(); | ||
} | ||
|
||
|
||
|
||
|
||
private static bool BooleanFunc() => DateTime.UtcNow.Millisecond % 2 == 0; | ||
private static string StringFunc() => Environment.MachineName; | ||
private static int Int32Func() => DateTime.UtcNow.Second; | ||
private static double DoubleFunc() => DateTime.UtcNow.Millisecond; | ||
|
||
private static void Action1() { } | ||
} |