From f974043a045291b2c955aa7fd5123b16b866c939 Mon Sep 17 00:00:00 2001 From: vivet Date: Wed, 12 Aug 2026 08:40:48 +0200 Subject: [PATCH] Improved exception handling for property type mismatch. Updated NuGets --- .github/workflows/build-and-deploy.yml | 2 +- .../Tests.DynamicExpression/CriteriaBuilderTest.cs | 14 ++++++++++++++ DynamicExpression/CriteriaBuilder.cs | 12 ++++++++++++ DynamicExpression/DynamicExpression.csproj | 3 ++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index 42a4a88..b490da1 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -8,7 +8,7 @@ on: - master env: APP_NAME: DynamicExpression - VERSION: 10.0.3 + VERSION: 10.0.4 NUGET_HOST: https://api.nuget.org/v3/index.json NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }} jobs: diff --git a/.tests/Tests.DynamicExpression/CriteriaBuilderTest.cs b/.tests/Tests.DynamicExpression/CriteriaBuilderTest.cs index 86077a9..a21c657 100644 --- a/.tests/Tests.DynamicExpression/CriteriaBuilderTest.cs +++ b/.tests/Tests.DynamicExpression/CriteriaBuilderTest.cs @@ -612,6 +612,20 @@ public void BuildWhenIntersectsTest() Assert.AreEqual("((x.Location != null) AndAlso x.Location.Intersects(POINT (0 0)))", expression.Body.ToString()); } + [TestMethod] + public void BuildWhenIntersectsAndPropertyTypeMismatchTest() + { + var criteriaExpression = new CriteriaExpression(); + + criteriaExpression.Intersects(nameof(Customer.Name), new Point(0, 0)); + + var exception = Assert.ThrowsExactly(() => CriteriaBuilder.Build(criteriaExpression)); + + Assert.IsInstanceOfType(exception.InnerException); + StringAssert.Contains(exception.Message, nameof(Customer.Name)); + StringAssert.Contains(exception.Message, nameof(OperationType.Intersects)); + } + [TestMethod] public void BuildWhenWithinTest() { diff --git a/DynamicExpression/CriteriaBuilder.cs b/DynamicExpression/CriteriaBuilder.cs index 9240c6a..565a0cf 100644 --- a/DynamicExpression/CriteriaBuilder.cs +++ b/DynamicExpression/CriteriaBuilder.cs @@ -112,6 +112,18 @@ private static Expression GetExpression(Expression parameter, Criteria criteria, } } + try + { + return CriteriaBuilder.GetExpressionCore(member, value, value2, operationType); + } + catch (Exception ex) when (ex is ArgumentException or InvalidOperationException && ex is not ArgumentOutOfRangeException) + { + throw new ArgumentException($"Criteria for property '{name}' cannot be applied: operation '{operationType}' with value type '{value.Type.Name}' is not compatible with the property's type '{member.Type.Name}'.", ex); + } + } + + private static Expression GetExpressionCore(Expression member, Expression value, Expression value2, OperationType operationType) + { if (value.Type.IsEnum) { var expression = Expression.Convert(member, Enum.GetUnderlyingType(value.Type)); diff --git a/DynamicExpression/DynamicExpression.csproj b/DynamicExpression/DynamicExpression.csproj index 85fba8c..6bbeb68 100644 --- a/DynamicExpression/DynamicExpression.csproj +++ b/DynamicExpression/DynamicExpression.csproj @@ -15,6 +15,7 @@ Lampda, Expression, Linq, Dynamic, Criteria, Query, Paging, Pagination, Sort, Sorting README.md + - Improved exception handling for property type mismatch. - Updated NuGets https://raw.githubusercontent.com/vivet/DynamicExpression/master/LICENSE @@ -47,7 +48,7 @@ - +