Hi Mark,

 

You are facing https://github.com/pythonnet/pythonnet/issues/514

At this moment the easiest way to do it is to add a method to ServiceInformation to add an item to SerivceHosts collection. E.g.

 

public void AddServiceHost(string serviceHost) => this.ServiceHosts.Add(serviceHost);

 

then call it from Python.

 

Regards,

Victor

 

From: Mark Brighton
Sent: Tuesday, March 3, 2020 10:42 AM
To: pythonnet@python.org
Subject: [Python.NET] Accessing a list that is a read-only property?

 

Hello,

 

I think this is a really basic question, but despite searching I haven't found a solution.  I've been asked to use a .NET assembly from python.  I have programed in other languages, but not C#.  In the brief time I've used it, PythonNet has been fantastic... with one little wrinkle. 

 

Here's a pared down version of the C# that I’m trying to access from Python:

 

namespace Service.API

{

    public class ServiceAPI

    {

        private static ServiceAPI m_Instance = new ServiceAPI();

 

        public static ServiceAPI Instance

        {

            get

            {

                return m_Instance;

            }

        }

       

        public ServiceAPI()

        {

        }

 

        ~ServiceAPI() {}

 

        ServiceInformation m_ServiceInformation = new ServiceInformation();

 

        public List<string> ServiceHosts

        {

            get

            {

                                                                return m_ServiceInformation.ServiceHosts;

            }

        }

                }

 

    public class ServiceInformation

    {

        public List<string> ServiceHosts { get; set; }

 

        public ServiceInformation()

        {

            ServiceHosts = new List<string>();

        }

                }

}

               

I need to add strings to the List “ServiceHosts”, accessing it via a ServiceAPI object.  The author placed the List inside another class, "ServiceInformation" and did not provide a set accessor, so that the ServiceHosts could not be altered to another object, while allowing strings to be added and removed from it. 

 

I think adding a string to that list is trivial in C#:

                ServiceAPI.Instance.ServiceHosts.Add('1.1.1.1');

 

So I try it in Python:

>>> import clr    # Pyton.NET package

>>> clr.AddReference("Service.API")      

>>> import Service.API

>>> import System         

>>> Service.API.ServiceAPI.Instance.ServiceHosts.Add('1.1.1.1')

Traceback (most recent call last):

  File "<interactive input>", line 1, in <module>

AttributeError: 'ServiceAPI' object has no attribute 'Add'

 

This is a List that PythonNet is mapping onto a python list:

>>> Service.API.ServiceAPI.Instance.ServiceHosts

[]

 

So try using a python method:

>>> Service.API.ServiceAPI.Instance.ServiceHosts.append('1.1.1.1')

>>> Service.API.ServiceAPI.Instance.ServiceHosts

[]

 

Nope.  And I start suspecting that I've added that string to a boxed version of the list.  But because there's no set accessor, I can't do this:

>>> hosts = Service.API.ServiceAPI.Instance.ServiceHosts

>>> hosts.append('1.1.1.1')

>>> Service.API.ServiceAPI.Instance.ServiceHosts = hosts

Traceback (most recent call last):

  File "<interactive input>", line 1, in <module>

TypeError: property is read-only

 

So what about calling the unbound list method, passing in the list?

>>> System.Collections.Generic.List[String].Add(

                                Service.API.ServiceAPI.Instance.ServiceHosts, '1.1.1.1')

Traceback (most recent call last):

  File "<interactive input>", line 1, in <module>

AttributeError: 'ServiceAPI' object has no attribute 'Add'

 

The author of this assembly has offered to create a specific method so that I can access the list, but he assures me that this is a common C# pattern that I will encounter frequently. 

 

I can't just create an instance of ServiceInformation and add the hosts to that, ServiceAPI has no method for getting or setting m_ServiceInformation. 

 

Any help in setting me in the right direction would be appreciated.

 

Thanks,

 

Mark Brighton

_______________________________________________

PythonNet mailing list -- pythonnet@python.org

To unsubscribe send an email to pythonnet-le...@python.org

https://mail.python.org/mailman3/lists/pythonnet.python.org/

 

_______________________________________________
PythonNet mailing list -- pythonnet@python.org
To unsubscribe send an email to pythonnet-le...@python.org
https://mail.python.org/mailman3/lists/pythonnet.python.org/

Reply via email to