Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genericmethods #2

Merged
merged 3 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,9 +1704,12 @@ Expression ParseMemberAccess(Type type, Expression expression)
throw ParseError(errorPos, Res.MethodsAreInaccessible, TypeHelper.GetTypeName(method.DeclaringType));
}

if (expression == null)
if (method.IsGenericMethod)
{
return Expression.Call(null, method, args);
var genericParameters = method.GetParameters().Where(p => p.ParameterType.IsGenericParameter);
var typeArguments = genericParameters.Select(a => args[a.Position].Type);
var constructedMethod = method.MakeGenericMethod(typeArguments.ToArray());
return Expression.Call(expression, constructedMethod, args);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionPromoter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ExpressionPromoter(ParsingConfig config)
/// <inheritdoc cref="IExpressionPromoter.Promote(Expression, Type, bool, bool)"/>
public virtual Expression Promote(Expression expr, Type type, bool exact, bool convertExpr)
{
if (expr.Type == type)
if (expr.Type == type || type.IsGenericParameter)
{
return expr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int FindMethod(Type type, string methodName, bool staticAccess, ref Expre
{
if (_parsingConfig.CustomTypeProvider.GetExtensionMethods().TryGetValue(t, out var extensionMethodsOfType))
{
methods.AddRange(extensionMethodsOfType.Where(m => m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase) && !m.IsGenericMethod));
methods.AddRange(extensionMethodsOfType.Where(m => m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public void DynamicExpressionParser_ParseLambda_Operator_Less_Greater_With_Guids

// Assert
Assert.Equal(anotherId, result);
}
}

[Theory]
[InlineData("c => c.Age == 8", "c => (c.Age == 8)")]
Expand Down Expand Up @@ -1239,7 +1239,7 @@ public void DynamicExpressionParser_ParseLambda_String_TrimEnd_0_Parameters()

var @delegate = expression.Compile();

var result = (bool) @delegate.DynamicInvoke("This is a test ");
var result = (bool)@delegate.DynamicInvoke("This is a test ");

// Assert
result.Should().BeTrue();
Expand All @@ -1258,5 +1258,36 @@ public void DynamicExpressionParser_ParseLambda_String_TrimEnd_1_Parameter()
// Assert
result.Should().BeTrue();
}

public class DefaultDynamicLinqCustomTypeProviderForGenericExtensionMethod : CustomTypeProviders.DefaultDynamicLinqCustomTypeProvider
{
public override HashSet<Type> GetCustomTypes() => new HashSet<Type>(base.GetCustomTypes()) { typeof(Methods), typeof(MethodsItemExtension) };
}

[Fact]
public void DynamicExpressionParser_ParseLambda_GenericExtensionMethod()
{
// Arrange
var testList = User.GenerateSampleModels(51);
var config = new ParsingConfig()
{
CustomTypeProvider = new DefaultDynamicLinqCustomTypeProviderForGenericExtensionMethod()
};

// Act
string query = "x => MethodsItemExtension.Functions.EfCoreCollate(x.UserName, \"tlh-KX\")==\"User4\" || MethodsItemExtension.Functions.EfCoreCollate(x.UserName, \"tlh-KX\")==\"User2\"";
var expression = DynamicExpressionParser.ParseLambda<User, bool>(config, false, query);
var del = expression.Compile();

var result = Enumerable.Where(testList, del);


var expected = testList.Where(x => new string[] { "User4", "User2" }.Contains(x.UserName)).ToList();

// Assert
Check.That(result).IsNotNull();
Check.That(result).HasSize(expected.Count);
Check.That(result).Equals(expected);
}
}
}
65 changes: 64 additions & 1 deletion test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq.Dynamic.Core.Exceptions;
Expand Down Expand Up @@ -1304,6 +1304,69 @@ public void ExpressionTests_Method_OneParam_With_it()
Assert.Equal(expected.Count(), result.Count());
}

public class DefaultDynamicLinqCustomTypeProviderForStaticTesting : CustomTypeProviders.DefaultDynamicLinqCustomTypeProvider
{
public override HashSet<Type> GetCustomTypes() => new HashSet<Type>(base.GetCustomTypes()) { typeof(Methods), typeof(MethodsItemExtension) };
}

[Fact]
public void ExpressionTests_MethodCall_GenericStatic()
{
var config = new ParsingConfig
{
CustomTypeProvider = new DefaultDynamicLinqCustomTypeProviderForStaticTesting()
};

// Arrange
var list = new[] { 0, 1, 2, 3, 4 }.Select(value => new Methods.Item { Value = value }).ToArray();

// Act
var expectedResult = list.Where(x => Methods.StaticGenericMethod(x));
var result = list.AsQueryable().Where(config, "Methods.StaticGenericMethod(it)");

// Assert
Assert.Equal(expectedResult.Count(), result.Count());
}

[Fact]
public void ExpressionTests_MethodCall_Generic()
{
var config = new ParsingConfig
{
CustomTypeProvider = new DefaultDynamicLinqCustomTypeProviderForStaticTesting()
};

// Arrange
var list = new[] { 0, 1, 2, 3, 4 }.Select(value => new Methods.Item { Value = value }).ToArray();

// Act
var methods = new Methods();
var expectedResult = list.Where(x => methods.GenericMethod(x));
var result = list.AsQueryable().Where("@0.GenericMethod(it)", methods);

// Assert
Assert.Equal(expectedResult.Count(), result.Count());
}

[Fact]
public void ExpressionTests_MethodCall_GenericExtension()
{
var config = new ParsingConfig
{
CustomTypeProvider = new DefaultDynamicLinqCustomTypeProviderForStaticTesting()
};

// Arrange
var list = new[] { 0, 1, 2, 3, 4 }.Select(value => new Methods.Item { Value = value }).ToArray();

// Act
var methods = new Methods();
var expectedResult = list.Where(x => MethodsItemExtension.Functions.EfCoreCollate(x.Value, "tlh-KX") == 2);
var result = list.AsQueryable().Where(config, "MethodsItemExtension.Functions.EfCoreCollate(it.Value,\"tlh-KX\")==2");

// Assert
Assert.Equal(expectedResult.Count(), result.Count());
}

[Fact]
public void ExpressionTests_MethodCall_ValueTypeToValueTypeParameter()
Expand Down
14 changes: 13 additions & 1 deletion test/System.Linq.Dynamic.Core.Tests/Helpers/Models/Methods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace System.Linq.Dynamic.Core.Tests.Helpers.Models
namespace System.Linq.Dynamic.Core.Tests.Helpers.Models
{
public class Methods
{
Expand All @@ -22,5 +22,17 @@ public class Item
{
public int Value { get; set; }
}

public static bool StaticGenericMethod<T>(T value) => value is Item item && item.Value == 1;
public bool GenericMethod<T>(T value) => value is Item item && item.Value == 1;

}

public static class MethodsItemExtension
{
public class DummyFunctions { }
public static DummyFunctions Functions => new DummyFunctions();

public static T EfCoreCollate<T>(this DummyFunctions _, T value, string collation) => value;
}
}