diff --git a/src/DynamicExpresso.Core/Resolution/MethodResolution.cs b/src/DynamicExpresso.Core/Resolution/MethodResolution.cs index a9506bb..a3ac76d 100644 --- a/src/DynamicExpresso.Core/Resolution/MethodResolution.cs +++ b/src/DynamicExpresso.Core/Resolution/MethodResolution.cs @@ -165,7 +165,12 @@ public static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Expr if (parameter.HasDefaultValue) { var parameterType = TypeUtils.GetConcreteTypeForGenericMethod(parameter.ParameterType, promotedArgs, method); - promotedArgs.Add(Expression.Constant(parameter.DefaultValue, parameterType)); + + var defaultValue = parameter.DefaultValue; + if (defaultValue is null && parameterType.IsValueType) + defaultValue = Activator.CreateInstance(parameterType); + + promotedArgs.Add(Expression.Constant(defaultValue, parameterType)); } else if (ReflectionExtensions.HasParamsArrayType(parameter)) { diff --git a/test/DynamicExpresso.UnitTest/GithubIssues.cs b/test/DynamicExpresso.UnitTest/GithubIssues.cs index 653e3c5..ea32bab 100644 --- a/test/DynamicExpresso.UnitTest/GithubIssues.cs +++ b/test/DynamicExpresso.UnitTest/GithubIssues.cs @@ -706,6 +706,11 @@ public List Add(List list, Func transform) { return list.Select(i => transform(i)).ToList(); } + + public Guid ReturnsGuid(Guid guid = default) + { + return guid; + } } [Test] @@ -877,6 +882,15 @@ public void GitHub_Issue_341() Assert.That(interpreter.Eval("(int)x == 1"), Is.EqualTo((int)x == 1)); } + + [Test] + public void GitHub_Issue_354() + { + var interpreter = new Interpreter(); + interpreter.SetVariable("b", new Functions()); + + Assert.That(interpreter.Eval("b.ReturnsGuid()"), Is.EqualTo(Guid.Empty)); + } } internal static class GithubIssuesTestExtensionsMethods