Skip to content
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
16 changes: 9 additions & 7 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2353,20 +2353,22 @@ private Expression ParseElementAccess(Expression expr)
switch (_methodFinder.FindIndexer(expr.Type, args, out var mb))
{
case 0:
throw ParseError(errorPos, Res.NoApplicableIndexer,
TypeHelper.GetTypeName(expr.Type));
throw ParseError(errorPos, Res.NoApplicableIndexer, TypeHelper.GetTypeName(expr.Type), args.Length);

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
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/Res.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal static class Res
public const string MissingAsClause = "Expression is missing an 'as' clause";
public const string NeitherTypeConvertsToOther = "Neither of the types '{0}' and '{1}' converts to the other";
public const string NewOperatorIsNotAllowed = "Using the new operator is not allowed via the ParsingConfig.";
public const string NoApplicableIndexer = "No applicable indexer exists in type '{0}'";
public const string NoApplicableIndexer = "No applicable indexer exists in type '{0}' with {1} parameters";
public const string NoApplicableMethod = "No applicable method '{0}' exists in type '{1}'";
public const string NoItInScope = "No 'it' is in scope";
public const string NoMatchingConstructor = "No matching constructor in type '{0}'";
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,79 @@ 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_IndexerParameterMismatch()
{
// 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")
};

Assert.Throws<ParseException>(() =>
DynamicExpressionParser.ParseLambda(config, false, expressionParams, null, "myObj[3,\"1\",1]"));
}

[Fact]
public void DynamicExpressionParser_ParseLambda_DuplicateParameterNames_ThrowsException()
{
Expand Down