Skip to content

Commit 712802a

Browse files
authored
Ensure action delegate allows call to void methods (#455)
1 parent 16cf068 commit 712802a

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,11 +1668,6 @@ Expression ParseMemberAccess(Type type, Expression instance)
16681668
throw ParseError(errorPos, Res.MethodsAreInaccessible, TypeHelper.GetTypeName(method.DeclaringType));
16691669
}
16701670

1671-
if (method.ReturnType == typeof(void))
1672-
{
1673-
throw ParseError(errorPos, Res.MethodIsVoid, id, TypeHelper.GetTypeName(method.DeclaringType));
1674-
}
1675-
16761671
if (instance == null)
16771672
{
16781673
return Expression.Call(null, method, args);

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,26 @@ public int Foo()
3636
return 42;
3737
}
3838

39+
public void Bar()
40+
{
41+
Name = nameof(Foo);
42+
}
43+
3944
public string Name { get; set; }
4045

4146
public MyClass Child { get; set; }
4247
}
4348

49+
private class MyClassCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
50+
{
51+
public override HashSet<Type> GetCustomTypes()
52+
{
53+
var customTypes = base.GetCustomTypes();
54+
customTypes.Add(typeof(MyClass));
55+
return customTypes;
56+
}
57+
}
58+
4459
private class ComplexParseLambda1Result
4560
{
4661
public int? Age;
@@ -1197,5 +1212,23 @@ public void DynamicExpressionParser_ParseLambda_NullPropagation_MethodCallExpres
11971212
// Assert
11981213
result.Should().BeNull();
11991214
}
1215+
1216+
[Fact]
1217+
public void DynamicExpressionParser_ParseLambda_ActionDelegate_VoidMethodCallExpression()
1218+
{
1219+
// Arrange
1220+
var dataSource = new MyClass();
1221+
var expressionText = "it.Bar()";
1222+
var parsingConfig = new ParsingConfig { CustomTypeProvider = new MyClassCustomTypeProvider() };
1223+
dataSource.Name.Should().BeNull();
1224+
1225+
// Act
1226+
LambdaExpression expression = DynamicExpressionParser.ParseLambda(typeof(Action<MyClass>), parsingConfig, dataSource.GetType(), null, expressionText);
1227+
Delegate del = expression.Compile();
1228+
del.DynamicInvoke(dataSource);
1229+
1230+
// Assert
1231+
dataSource.Name.Should().NotBeNullOrEmpty();
1232+
}
12001233
}
12011234
}

0 commit comments

Comments
 (0)