Skip to content

Type parameter works directly, but fails inside List<> #962

Closed

Description

Environment

  • Pythonnet version: 2.4.0
  • Python version: Python 3.7.4
  • Operating System: Windows 10 (18362.356)

Details

  • Describe what you were trying to get done.

Call a generic method taking a list parameter.

C# library:

using System.Collections.Generic;

namespace CSLib
{
    public static class CSLib
    {
        public static bool GetBool() =>
            true;

        public static void TestValue<T>(T t) {}

        public static List<bool> GetBoolList() =>
            new List<bool>() { true, false };

        public static void TestList<T>(List<T> list) {}

        public static void Test()
        {
            TestValue<bool>(GetBool());
            TestList<bool>(GetBoolList());
        }
    }
}

Python code:

from clr import AddReference
from pathlib import Path

AddReference(str(Path.cwd() / "cslib/bin/Debug/netstandard2.0/cslib.dll"))

from System import Boolean
from CSLib import CSLib

CSLib.TestValue[Boolean](CSLib.GetBool())
CSLib.TestList[Boolean](CSLib.GetBoolList())

It works fine when the type argument is used directly, but fails when it is inside List<>.

  • If there was a crash, please include the traceback here.
Traceback (most recent call last):
  File "a.py", line 11, in <module>
    CSLib.TestList[Boolean](CSLib.GetBoolList())
TypeError: No method matches given arguments for TestList

Activity

  1. filmor commented on Sep 27, 2019

    @filmor
    Member

    This is due to the implicit conversion of .NET lists to Python lists again. Since we don't do it the other way round (and it would also be very tricky to get that right), when you get CSLib.GetBoolList() you end up with a Python list [True] which doesn't match the method signature.

    I'm working on removing the implicit conversion in #963.

  2. lostmsu commented on Sep 23, 2021

    @lostmsu
    Member

    This should now be fixed in 3.0

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