Skip to content

Commit 70a3298

Browse files
authored
DynamicLinqTypeAttribute can now also be defined on an interface (#750)
1 parent c4b99be commit 70a3298

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
namespace System.Linq.Dynamic.Core.CustomTypeProviders
1+
namespace System.Linq.Dynamic.Core.CustomTypeProviders;
2+
3+
/// <summary>
4+
/// Indicates to Dynamic Linq to consider the Type as a valid dynamic linq type.
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
7+
public sealed class DynamicLinqTypeAttribute : Attribute
28
{
3-
/// <summary>
4-
/// Indicates to Dynamic Linq to consider the Type as a valid dynamic linq type.
5-
/// </summary>
6-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
7-
public sealed class DynamicLinqTypeAttribute : Attribute
8-
{
9-
}
10-
}
9+
}

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public class CustomClassWithMethodWithDynamicLinqTypeAttribute
8888
public int GetAge(int x) => x;
8989
}
9090

91+
[DynamicLinqType]
92+
public interface ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute
93+
{
94+
int GetAge(int x);
95+
}
96+
97+
public class CustomClassImplementingInterface : ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute
98+
{
99+
public int GetAge(int x) => x;
100+
}
101+
91102
[DynamicLinqType]
92103
public class CustomClassWithStaticMethodWithDynamicLinqTypeAttribute
93104
{
@@ -1057,7 +1068,7 @@ public void DynamicExpressionParser_ParseLambda_CustomStaticMethod_WhenClassHasD
10571068
// Act
10581069
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithStaticMethodWithDynamicLinqTypeAttribute), null, expression);
10591070
var del = lambdaExpression.Compile();
1060-
var result = (int)del.DynamicInvoke(context);
1071+
var result = (int?)del.DynamicInvoke(context);
10611072

10621073
// Assert
10631074
Check.That(result).IsEqualTo(10);
@@ -1073,12 +1084,28 @@ public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassHasDynamic
10731084
// Act
10741085
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithMethodWithDynamicLinqTypeAttribute), null, expression);
10751086
var del = lambdaExpression.Compile();
1076-
var result = (int)del.DynamicInvoke(context);
1087+
var result = (int?)del.DynamicInvoke(context);
10771088

10781089
// Assert
10791090
Check.That(result).IsEqualTo(10);
10801091
}
10811092

1093+
[Fact]
1094+
public void DynamicExpressionParser_ParseLambda_CustomInterface_WhenInterfaceHasDynamicLinqTypeAttribute_ShouldWorkCorrect()
1095+
{
1096+
// Arrange
1097+
var context = new CustomClassImplementingInterface();
1098+
var expression = $"{nameof(ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute.GetAge)}(10)";
1099+
1100+
// Act
1101+
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute), null, expression);
1102+
var del = lambdaExpression.Compile();
1103+
var result = (int?)del.DynamicInvoke(context);
1104+
1105+
// Assert
1106+
result.Should().Be(10);
1107+
}
1108+
10821109
[Fact]
10831110
public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassDoesNotHaveDynamicLinqTypeAttribute_ShouldThrowException()
10841111
{

0 commit comments

Comments
 (0)