Skip to content

pythonnet 2.4dev fails to add items to Generic List #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjmaenpaa opened this issue Jun 5, 2018 · 1 comment
Closed

pythonnet 2.4dev fails to add items to Generic List #681

mjmaenpaa opened this issue Jun 5, 2018 · 1 comment

Comments

@mjmaenpaa
Copy link

Environment

  • Pythonnet version: 2.4 dev, commit f0f6b6b
  • Python version: 2.7 32bit
  • Operating System: Windows 10

Details

  • Describe what you were trying to get done:
    Adding items to a C# generic list inside C# class using Python fails. List is a class instance variable which is exposed outside the class using getter/setter. In pythonnet 2.3 & IronPython list inside class is exposed as C# list to Python code. However starting with pythonnet 2.4 it is exposed as python list. Adding items to that list works fine on pythonnet 2.3 & IronPython but not with pythonnet 2.4.

  • What commands did you run to trigger this issue?

Minimal example C# class. Save as example_dll_limited.cs:

namespace example_dll_limited {
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class CollectionClass {
        
        private List<string> sequenceItemField;
        
        public CollectionClass() {
            this.sequenceItemField = new List<string>();
        }

        public int SizeCheck() {
            return this.sequenceItemField.Count;
        }

        public List<string> SequenceItem {
            get {
                return this.sequenceItemField;
            }
            set {
                this.sequenceItemField = value;
            }
        }
    }
}

Generate library dll from example class using csc:

csc.exe /t:library example_dll_limited.cs
# Or with full path to csc if not in path:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:library example_dll_limited.cs

Execute following python commands which instantiate single instance of class and tries to add two elements to the list inside the class:

from __future__ import print_function
import clr

clr.AddReference('example_dll_limited')
import example_dll_limited

# Create object
object_ = example_dll_limited.CollectionClass()
# prints Python list on 2.4, C# List on python 2.3
print(type(object_.SequenceItem))

if not hasattr(clr, '__version__') or clr.__version__.startswith('2.3'):
    # This one works correctly with IronPython & pythonnet 2.3
    object_.SequenceItem.Add('first_item')
    object_.SequenceItem.Add('second_item')
    size = object_.SequenceItem.Count
else:
    # Pythonnet 2.4 seems to do implicit list conversion
    # This one doesn't add anything to python list with pythonnet 2.4dev
    retval = object_.SequenceItem.append('first_item')
    object_.SequenceItem.append('second_item')
    size = len(object_.SequenceItem)

size_explicit = object_.SizeCheck()

print('List should have 2 items')
print('Size check: {}'.format(size))
print('Explicit method call size check: {}'.format(size_explicit))

Output using pythonnet 2.3:

<class 'System.Collections.Generic.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'>
List should have 2 items
Size check: 2
Explicit method call size check: 2

Output using pythonnet 2.4dev

<type 'list'>
List should have 2 items
Size check: 0
Explicit method call size check: 0
  • If there was a crash, please include the traceback here.
    No crash
@den-run-ai
Copy link
Contributor

@mjmaenpaa this looks like a duplicate:

#514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants