dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
1.91k stars 364 forks source link

Indexer ambiguity when inherited class replaces parent's indexer with new keyword #305

Closed denbell5 closed 2 months ago

denbell5 commented 3 months ago

Hello!

First of all, thanks to all of the contributors for this project.

This is a very rare case, but it exists in our project. The issue happens when descendant class overrides indexer with new keyword and sets a different return type. In this case, Parser finds two applicable methods, calls MethodHasPriority, and then fails here because MethodBase is null for IndexerData.

Unit test code to reproduce:

public class A
{
    public string this[int index] => "some string";
}

public class B : A
{
    public new int this[int index] => 25;
}

[Test]
public void Test()
{
    var b = new B();

    var interpreter = new Interpreter();
    var lambda = interpreter.Parse("this[0]", new Parameter("this", b));
    var res = lambda.Invoke(b);
    Assert.AreEqual(25, res);
}

I investigated the code, and, if this is ok, I would like to create a pull request with suggested solution for this issue.