From db5749b4747ab3ef7e7ab7d4b78f7385b9329031 Mon Sep 17 00:00:00 2001 From: Martin Smith Date: Thu, 29 Aug 2024 19:22:34 +0100 Subject: [PATCH] stash --- .../Reflection/ExpressionExtensions.cs | 148 ++++++++++++++++++ .../Reflection/ExpressionExtensionsTests.cs | 59 +++++++ 2 files changed, 207 insertions(+) create mode 100644 src/DNX.Extensions/Reflection/ExpressionExtensions.cs create mode 100644 tests/DNX.Extensions.Tests/Reflection/ExpressionExtensionsTests.cs diff --git a/src/DNX.Extensions/Reflection/ExpressionExtensions.cs b/src/DNX.Extensions/Reflection/ExpressionExtensions.cs new file mode 100644 index 0000000..88281c6 --- /dev/null +++ b/src/DNX.Extensions/Reflection/ExpressionExtensions.cs @@ -0,0 +1,148 @@ +using System; +using System.Linq.Expressions; + +namespace DNX.Extensions.Reflection +{ + /// + /// Class ExpressionExtensions. + /// + public static class ExpressionExtensions + { + /// + /// Determines whether the func is a member expression + /// + /// + /// The func. + /// + public static bool IsMemberExpression(this Expression> func) + { + return func.Body is MemberExpression; + } + + /// + /// Determines whether the action is a member expression + /// + /// + /// The action. + /// + public static bool IsMemberExpression(this Expression action) + { + return action.Body is MemberExpression; + } + + /// + /// Determines whether the func is a lambda expression + /// + /// + /// The func. + /// + public static bool IsLambdaExpression(Expression> func) + { + return func.Body is LambdaExpression; + } + + /// + /// Determines whether the action is a lambda expression + /// + /// + /// The action. + /// + public static bool IsLambdaExpression(Expression> action) + { + return action.Body is LambdaExpression; + } + + /// + /// Determines whether the func is a unary expression + /// + /// + /// The func. + /// + public static bool IsUnaryExpression(Expression> func) + { + var unaryExpression = func.Body as UnaryExpression; + + return unaryExpression != null; + } + + /// + /// Determines whether the action is a unary expression + /// + /// + /// The action. + /// + public static bool IsUnaryExpression(Expression> action) + { + var unaryExpression = action.Body as UnaryExpression; + + return unaryExpression != null; + } + + /// + /// Gets the name of the member expression. + /// + /// + /// The func. + /// + public static string GetMemberName(Expression> func) + { + var memberExpression = func.Body as MemberExpression; + + return memberExpression?.Member.Name; + } + + /// + /// Gets the name of the lambda expression. + /// + /// + /// The exp. + /// + public static string GetLambdaName(Expression> exp) + { + var lambdaExpression = exp.Body as LambdaExpression; + + return lambdaExpression?.Name; + } + + /// + /// Gets the name of the unary expression. + /// + /// + /// The exp. + /// + public static string GetUnaryName(Expression> exp) + { + var unaryExpression = exp.Body as UnaryExpression; //((UnaryExpression)exp.Body).Operand; + + var operand = unaryExpression?.Operand as MemberExpression; + + return operand?.Member.Name; + } + + /// + /// Gets the name of the expression. + /// + /// + /// The exp. + /// + public static string GetExpressionName(Expression> exp) + { + if (IsMemberExpression(exp)) + { + return GetMemberName(exp); + } + + if (IsLambdaExpression(exp)) + { + return GetLambdaName(exp); + } + + if (IsUnaryExpression(exp)) + { + return GetUnaryName(exp); + } + + return null; + } + } +} diff --git a/tests/DNX.Extensions.Tests/Reflection/ExpressionExtensionsTests.cs b/tests/DNX.Extensions.Tests/Reflection/ExpressionExtensionsTests.cs new file mode 100644 index 0000000..3279413 --- /dev/null +++ b/tests/DNX.Extensions.Tests/Reflection/ExpressionExtensionsTests.cs @@ -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(() => 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() { } +}