Skip to content

No way of Invoking a generic method with multiple overload and out/byref parameters #641

Open

Description

@RockHornetz

Environment

  • Pythonnet version: 2.4.0.dev0
  • Python version: 3.6.4
  • Operating System: Windows 7 64

Details

  • Describe what you were trying to get done.

    Invoke a generic method with multiple overload and out parameters

  • What commands did you run to trigger this issue?

//C# SIDE
//...
    public void GetProperty<T>(string propertyName, out T value){...} //<= The one i'm trying to get
    public void GetProperty<T>(string propertyName, out T value, bool asRef){...}
    public void GetProperty<T>(string propertyName, out T value, int index){...}
    public void GetProperty<T>(string propertyName, out T value, int index, bool asRef){...}
    public void GetProperty<T>(PropertyID propertyID, out T value, int index){...}
    public void GetProperty<T>(PropertyID propertyID, out T value, bool asRef){...}
//...
#PYTHON SIDE, What i've tested so far
object.GetProperty[System.Single]('PropertyName', None) #FAIL
object.GetProperty[System.Single]('PropertyName', System.Single(0).GetType()) #FAIL
object.GetProperty[System.Single]('PropertyName', System.Single(0).GetType().MakeByRefType()) #FAIL

object.GetProperty('PropertyName', None) #FAIL
object.GetProperty('PropertyName', System.Single(0).GetType()) #FAIL
object.GetProperty('PropertyName', System.Single(0).GetType().MakeByRefType()) #FAIL

object.GetProperty.Overloads[System.String, System.Single(0).GetType().MakeByRefType()]('PropertyName', None) #FAIL
  • Traceback:
   Traceback (most recent call last):
  File "C:\Users\{USER}\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\{USER}\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:/PythonWorkspace/{MODULE}\__main__.py", line 5, in <module>
    test_pythonnet.launchTest()
  File "D:/PythonWorkspace/{MODULE}\test_pythonnet.py", line 45, in launchTest
    test = object.GetProperty[System.Single]('PropertyName', byref)
TypeError: No method matches given arguments

So, i'm at a loss here, do i need to do all the reflection stuff by hand ?

Activity

  1. den-run-ai commented on Mar 5, 2018

    @den-run-ai
    Contributor

    @llezo you may want to have a look at this issue. I don't see any way to handle byref overloads reliably without re-writing the method resolution logic:

    #265

  2. RockHornetz commented on Mar 5, 2018

    @RockHornetz
    Author

    That's what i thought.
    I will try some hack on my side, because that sort of problem is everywhere on the DLLs i work with
    Here is what i think i'll try:

    #Access by its position in the list of overloads
    object.GetProperty.Overloads.ByIndex[0] #GetProperty<T>(string propertyName, out T value)
    #By passing a string that is equal to GetParameters().ParameterType.Name
    object.GetProperty.Overloads.BySignature["System.String","T&", "System.Boolean&"] #GetProperty<T>(string propertyName, out T value, bool asRef)
    #Eventually make this work:
    SingleByRef = System.Type.GetType(System.Single).MakeByRefType()
    object.GetProperty[System.Single]('PropertyName', SingleByRef)

    What do you think?

  3. den-run-ai commented on Mar 5, 2018

    @den-run-ai
    Contributor

    @llezo i put something together here long ago, but be careful because in some edge cases this would pick the wrong overloaded method, e.g. when both byref and non-byref versions are present:

    https://github.com/pythonnet/pythonnet/pull/227/files

  4. RockHornetz commented on Mar 5, 2018

    @RockHornetz
    Author

    Hmm it should do the trick but i'll keep an eye on what i'm calling.
    Thanks !

  5. williamsardar commented on Apr 23, 2018

    @williamsardar
    Contributor

    There is a more general issue here, that you can't choose a specific overload of a generic method. It always matches the first in MethodBinder.MatchParameters

    public void GetProperty(string propertyName, T value){...} //<= The one i'm trying to get
    public void GetProperty(double propertyName, T value){...}

  6. Tobimaru commented on Feb 24, 2019

    @Tobimaru

    Hi,

    Is there any progress on resolving this issue? I have encountered a similar problem when trying out pythonnet with Infer.Net. So in my case I have on the C# side generic overloads that have signatures:

    Variable<T> Variable.Constant<T>(T)
    VariableArray<T> Variable.Constant<T>(T[])
    

    I'm trying to call the second overload in pythonnet as:

    a = Variable.Constant(System.Array[System.Double]([1.0, 2.0, 3.0]))
    

    But when I check the type of a, it gives Variable[System.Double[]] and not VariableArray[System.Double]. This makes sense since it's ambiguous without specifying the type of the output variable or specifying the type for the function. Since in python I can't pin the output variable to a specific type, I tried to resolve the ambiguity with:

    a = Variable.Constant[System.Double](System.Array[System.Double]([1.0, 2.0, 3.0])
    

    but then I get the exception:

    TypeError: No method matches given arguments for Constant
    

    It seems that there is pull-request (#227) ongoing, but it looks to have been work in progress for quite a while now. Is there someone working on resolving this issue? If not, is there a work-around that I could try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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