diff --git a/GitVersion.yml b/GitVersion.yml index 47c3fc8..cc09407 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1 +1 @@ -next-version: 8.0.0 \ No newline at end of file +next-version: 8.2.0 \ No newline at end of file diff --git a/src/Fluxera.ComponentModel.Annotations/Guard.cs b/src/Fluxera.ComponentModel.Annotations/Guard.cs index 17f48af..7eebe0c 100644 --- a/src/Fluxera.ComponentModel.Annotations/Guard.cs +++ b/src/Fluxera.ComponentModel.Annotations/Guard.cs @@ -3,7 +3,9 @@ using System; using System.Collections.Generic; using System.Linq; +#if NET7_0_OR_GREATER using System.Numerics; +#endif using System.Runtime.CompilerServices; using JetBrains.Annotations; @@ -28,7 +30,7 @@ public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][ return argument; } - public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) + public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) { argument = ThrowIfNull(argument, parameterName); diff --git a/src/Fluxera.Linq.Expressions/Evaluator.cs b/src/Fluxera.Linq.Expressions/Evaluator.cs index a1b5dda..41c4262 100644 --- a/src/Fluxera.Linq.Expressions/Evaluator.cs +++ b/src/Fluxera.Linq.Expressions/Evaluator.cs @@ -13,7 +13,7 @@ /// Copyright notice http://msdn.microsoft.com/en-gb/cc300389.aspx#O /// [PublicAPI] - public static class Evaluator + internal static class Evaluator { /// /// Performs evaluation and replacement of independent sub-trees. @@ -76,6 +76,12 @@ public override Expression Visit(Expression exp) return base.Visit(exp); } + /// + protected override Expression VisitMemberInit(MemberInitExpression node) + { + return node; + } + private Expression Evaluate(Expression e) { if(e.NodeType == ExpressionType.Constant) diff --git a/src/Fluxera.Linq.Expressions/LocalCollectionExpander.cs b/src/Fluxera.Linq.Expressions/LocalCollectionExpander.cs index 676ba58..9ed577d 100644 --- a/src/Fluxera.Linq.Expressions/LocalCollectionExpander.cs +++ b/src/Fluxera.Linq.Expressions/LocalCollectionExpander.cs @@ -15,7 +15,7 @@ /// See: http://petemontgomery.wordpress.com/2008/08/07/caching-the-results-of-linq-queries/ /// [PublicAPI] - public sealed class LocalCollectionExpander : ExpressionVisitor + internal sealed class LocalCollectionExpander : ExpressionVisitor { /// /// Rewrites the given expression by expanding local collections. diff --git a/src/Fluxera.Linq.Expressions/ParameterRebinder.cs b/src/Fluxera.Linq.Expressions/ParameterRebinder.cs index 944bdce..21172b7 100644 --- a/src/Fluxera.Linq.Expressions/ParameterRebinder.cs +++ b/src/Fluxera.Linq.Expressions/ParameterRebinder.cs @@ -12,7 +12,7 @@ /// http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx. /// [PublicAPI] - public sealed class ParameterRebinder : ExpressionVisitor + internal sealed class ParameterRebinder : ExpressionVisitor { private readonly Dictionary map; diff --git a/src/Fluxera.Linq.Expressions/UtilExtensions.cs b/src/Fluxera.Linq.Expressions/UtilExtensions.cs index 67d1f70..f6097ec 100644 --- a/src/Fluxera.Linq.Expressions/UtilExtensions.cs +++ b/src/Fluxera.Linq.Expressions/UtilExtensions.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using System.Linq; using System.Text; internal static class UtilExtensions @@ -28,7 +27,7 @@ public static string ToConcatenatedString(this IEnumerable source, Func ToLinkedList(this IEnumerable enumerable) { - enumerable ??= Enumerable.Empty(); + enumerable ??= []; return new LinkedList(enumerable); } diff --git a/src/Fluxera.Utilities/Guard.cs b/src/Fluxera.Utilities/Guard.cs index 6344206..f090b2a 100644 --- a/src/Fluxera.Utilities/Guard.cs +++ b/src/Fluxera.Utilities/Guard.cs @@ -4,7 +4,9 @@ using System; using System.Collections.Generic; using System.Linq; +#if NET7_0_OR_GREATER using System.Numerics; +#endif using System.Runtime.CompilerServices; internal static class Guard @@ -28,7 +30,7 @@ public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][ return argument; } - public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) + public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) { argument = Guard.ThrowIfNull(argument, parameterName); diff --git a/tests/Fluxera.Linq.Expressions.UnitTests/ExpressionExtensionsTests.cs b/tests/Fluxera.Linq.Expressions.UnitTests/ExpressionExtensionsTests.cs index a530507..853a7b7 100644 --- a/tests/Fluxera.Linq.Expressions.UnitTests/ExpressionExtensionsTests.cs +++ b/tests/Fluxera.Linq.Expressions.UnitTests/ExpressionExtensionsTests.cs @@ -2,6 +2,8 @@ { using System; using System.Linq.Expressions; + using System.Reflection; + using DynamicAnonymousType; using FluentAssertions; using NUnit.Framework; @@ -48,5 +50,45 @@ public void ShouldCreateExpressionStringForSimplePropertyExpression() result.Should().NotBeNullOrWhiteSpace(); result.Should().Be("x => x.Name"); } + + [Test] + public void ShouldCreateExpressionStringForSelectionExpression() + { + // Arrange + Expression> expression = x => new { x.Name }; + + // Act + string result = expression.ToExpressionString(); + + // Assert + result.Should().NotBeNullOrWhiteSpace(); + result.Should().Be("x => new <>f__AnonymousType0`1(Name = x.Name)"); + } + + [Test] + public void ShouldCreateExpressionStringForDynamicSelectionExpression() + { + // Arrange + Type type = DynamicFactory.CreateType(("Name", typeof(string))); + + PropertyInfo property = typeof(Person).GetProperty("Name"); + PropertyInfo dynamicProperty = type.GetProperty("Name"); + + ParameterExpression parameter = Expression.Parameter(typeof(Person), "x"); + MemberExpression propertyExpression = Expression.MakeMemberAccess(parameter, property); + MemberAssignment binding = Expression.Bind(dynamicProperty, propertyExpression); + + NewExpression newExpression = Expression.New(type); + MemberInitExpression memberInitExpression = Expression.MemberInit(newExpression, binding); + + Expression> expression = Expression.Lambda>(memberInitExpression, parameter); + + // Act + string result = expression.ToExpressionString(); + + // Assert + result.Should().NotBeNullOrWhiteSpace(); + result.Should().Be("x => new DynamicAnonymousType0`1() {Name = x.Name}"); + } } } diff --git a/tests/Fluxera.Linq.Expressions.UnitTests/Fluxera.Linq.Expressions.UnitTests.csproj b/tests/Fluxera.Linq.Expressions.UnitTests/Fluxera.Linq.Expressions.UnitTests.csproj index d7cacb7..4d1930d 100644 --- a/tests/Fluxera.Linq.Expressions.UnitTests/Fluxera.Linq.Expressions.UnitTests.csproj +++ b/tests/Fluxera.Linq.Expressions.UnitTests/Fluxera.Linq.Expressions.UnitTests.csproj @@ -6,6 +6,7 @@ + diff --git a/tests/Fluxera.Linq.Expressions.UnitTests/Person.cs b/tests/Fluxera.Linq.Expressions.UnitTests/Person.cs index 3133b29..71fc06e 100644 --- a/tests/Fluxera.Linq.Expressions.UnitTests/Person.cs +++ b/tests/Fluxera.Linq.Expressions.UnitTests/Person.cs @@ -3,5 +3,7 @@ public class Person { public string Name { get; set; } + + public int Age { get; set; } } }