Skip to content

Commit

Permalink
DynamicLinqTypeAttribute can now also be defined on an interface (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Oct 14, 2023
1 parent c4b99be commit 70a3298
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace System.Linq.Dynamic.Core.CustomTypeProviders
namespace System.Linq.Dynamic.Core.CustomTypeProviders;

/// <summary>
/// Indicates to Dynamic Linq to consider the Type as a valid dynamic linq type.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
public sealed class DynamicLinqTypeAttribute : Attribute
{
/// <summary>
/// Indicates to Dynamic Linq to consider the Type as a valid dynamic linq type.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
public sealed class DynamicLinqTypeAttribute : Attribute
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ public class CustomClassWithMethodWithDynamicLinqTypeAttribute
public int GetAge(int x) => x;
}

[DynamicLinqType]
public interface ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute
{
int GetAge(int x);
}

public class CustomClassImplementingInterface : ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute
{
public int GetAge(int x) => x;
}

[DynamicLinqType]
public class CustomClassWithStaticMethodWithDynamicLinqTypeAttribute
{
Expand Down Expand Up @@ -1057,7 +1068,7 @@ public void DynamicExpressionParser_ParseLambda_CustomStaticMethod_WhenClassHasD
// Act
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithStaticMethodWithDynamicLinqTypeAttribute), null, expression);
var del = lambdaExpression.Compile();
var result = (int)del.DynamicInvoke(context);
var result = (int?)del.DynamicInvoke(context);

// Assert
Check.That(result).IsEqualTo(10);
Expand All @@ -1073,12 +1084,28 @@ public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassHasDynamic
// Act
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(CustomClassWithMethodWithDynamicLinqTypeAttribute), null, expression);
var del = lambdaExpression.Compile();
var result = (int)del.DynamicInvoke(context);
var result = (int?)del.DynamicInvoke(context);

// Assert
Check.That(result).IsEqualTo(10);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_CustomInterface_WhenInterfaceHasDynamicLinqTypeAttribute_ShouldWorkCorrect()
{
// Arrange
var context = new CustomClassImplementingInterface();
var expression = $"{nameof(ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute.GetAge)}(10)";

// Act
var lambdaExpression = DynamicExpressionParser.ParseLambda(typeof(ICustomInterfaceWithMethodWithDynamicLinqTypeAttribute), null, expression);
var del = lambdaExpression.Compile();
var result = (int?)del.DynamicInvoke(context);

// Assert
result.Should().Be(10);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_CustomMethod_WhenClassDoesNotHaveDynamicLinqTypeAttribute_ShouldThrowException()
{
Expand Down

0 comments on commit 70a3298

Please sign in to comment.