Skip to content

Method precedence #552

@AlexCatarino

Description

@AlexCatarino

Environment

  • Pythonnet version: 2.4.0
  • Python version: 2.7
  • Operating System: Windows 10

Details

We have a series of overloads for a given method in existing base class and we want to add new overloads that should take precedence. To be more precise, we have the following case:
public IEnumerable<Slice> History(int periods)
that is consumed by C# classes and we have
public PyObject History(int periods)
that is consumed by Python classes
To achieve it, we have created a subclass where the second overload is defined:

public class QCAlgorithm
{
    public IEnumerable<Slice> History(int periods) 
    {
    }
}
public class QCPyAlgorithm : QCAlgorithm
{
    public new PyObject History(int periods) 
    {
    }
}

When we use reflection to get the methods from QCPyAlgorithm, the new method comes first. However, after the sorting performed by pythonnet here, the method from QCAlgorithm comes first. Consequently it is called in my python script and returns an enumerable of Slice.

The issue/question is:
How do I keep the methods from the declaring type on the top of the list of methods are try to bind?

My first guess is to send the first overload behind by adding the following line in the GetPrecedence method:

val += mi.DeclaringType == mi.ReflectedType ? 0 : 3000;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions