Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,15 +2358,18 @@ private Expression ParseElementAccess(Expression expr)

case 1:
var indexMethod = (MethodInfo)mb!;
var indexParameterType = indexMethod.GetParameters().First().ParameterType;
var indexMethodArguments = indexMethod.GetParameters();

var indexArgumentExpression = args[0]; // Indexer only has 1 parameter, so we can use args[0] here
if (indexParameterType != indexArgumentExpression.Type)
var indexArgumentExpressions = new Expression[args.Length];
for (var i = 0; i < indexMethodArguments.Length; ++i)
{
indexArgumentExpression = Expression.Convert(indexArgumentExpression, indexParameterType);
var indexParameterType = indexMethodArguments[i].ParameterType;
indexArgumentExpressions[i] = indexParameterType != args[i].Type
? Expression.Convert(args[i], indexParameterType)
: args[i];
}

return Expression.Call(expr, indexMethod, indexArgumentExpression);
return Expression.Call(expr, indexMethod, indexArgumentExpressions);

default:
throw ParseError(errorPos, Res.AmbiguousIndexerInvocation, TypeHelper.GetTypeName(expr.Type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public override HashSet<Type> GetCustomTypes()
}
}

private class ClassWithIndexers
{
public int this[int i1]
{
get => i1 + 1;
}
public string this[int i1, string i2]
{
get => i1 + "-" + i2;
}
}

private class ComplexParseLambda1Result
{
public int? Age;
Expand Down Expand Up @@ -660,6 +672,60 @@ public void DynamicExpressionParser_ParseLambda_Issue58()
Check.That(result).Equals(42);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_Indexer1D()
{
// Arrange
var customTypeProvider = new Mock<IDynamicLinqCustomTypeProvider>();
customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(ClassWithIndexers)]);
var config = new ParsingConfig
{
CustomTypeProvider = customTypeProvider.Object
};
var expressionParams = new[]
{
Expression.Parameter(typeof(ClassWithIndexers), "myObj")
};

var myClassInstance = new ClassWithIndexers();
var invokersMerge = new List<object> { myClassInstance };

// Act
var expression = DynamicExpressionParser.ParseLambda(config, false, expressionParams, null, "myObj[3]");
var del = expression.Compile();
var result = del.DynamicInvoke(invokersMerge.ToArray());

// Assert
Check.That(result).Equals(4);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_Indexer2D()
{
// Arrange
var customTypeProvider = new Mock<IDynamicLinqCustomTypeProvider>();
customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(ClassWithIndexers)]);
var config = new ParsingConfig
{
CustomTypeProvider = customTypeProvider.Object
};
var expressionParams = new[]
{
Expression.Parameter(typeof(ClassWithIndexers), "myObj")
};

var myClassInstance = new ClassWithIndexers();
var invokersMerge = new List<object> { myClassInstance };

// Act
var expression = DynamicExpressionParser.ParseLambda(config, false, expressionParams, null, "myObj[3,\"1\"]");
var del = expression.Compile();
var result = del.DynamicInvoke(invokersMerge.ToArray());

// Assert
Check.That(result).Equals("3-1");
}

[Fact]
public void DynamicExpressionParser_ParseLambda_DuplicateParameterNames_ThrowsException()
{
Expand Down
Loading