Skip to content

Commit

Permalink
Can set nullable values from LambdaBuilder. 1.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jun 10, 2024
1 parent 32624d4 commit 859ffd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/JasperFx.Core.Tests/Reflection/LambdaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void can_build_setter_for_property()

target.Number.ShouldBe(11);
}

[Fact]
public void can_build_setter_for_nullable_property()
{
var target = new Target { NullableNumber = 5 };
var prop = ReflectionHelper.GetProperty<Target>(x => x.NullableNumber);

var setter = LambdaBuilder.SetProperty<Target, int>(prop);

setter(target, 11);


target.NullableNumber.Value.ShouldBe(11);
}


[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/JasperFx.Core/JasperFx.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Common extension methods and reflection helpers used by JasperFx projects</Description>
<Version>1.7.1</Version>
<Version>1.7.2</Version>
<Authors>Jeremy D. Miller</Authors>
<AssemblyName>JasperFx.Core</AssemblyName>
<PackageId>JasperFx.Core</PackageId>
Expand Down
4 changes: 3 additions & 1 deletion src/JasperFx.Core/Reflection/LambdaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public static Func<TTarget, TProperty> GetProperty<TTarget, TProperty>(PropertyI
var method = property.SetMethod;

if (method == null) return null;

Expression actualValue = property.PropertyType == typeof(TProperty) ? value : Expression.Convert(value, property.PropertyType);

var callSetMethod = Expression.Call(target, method, value);
var callSetMethod = Expression.Call(target, method, actualValue);

var lambda = Expression.Lambda<Action<TTarget, TProperty>>(callSetMethod, target, value);

Expand Down

0 comments on commit 859ffd7

Please sign in to comment.