[Python.NET] Accessing a list that is a read-only property?

2020-03-03 Thread Mark Brighton
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 ServiceHosts
{
get
{
return m_ServiceInformation.ServiceHosts;
}
}
}

public class ServiceInformation 
{
public List ServiceHosts { get; set; }

public ServiceInformation()
{
ServiceHosts = new List();
}
}
}

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 "", line 1, in 
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 "", line 1, in 
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 "", line 1, in 
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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/


[Python.NET] How do you add an element to a List in a read-only property?

2020-03-03 Thread mark
Hello,

I think this is a really basic question, but 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#:

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 ServiceHosts
{
get
{
return m_ServiceInformation.ServiceHosts;
}
}
}

public class ServiceInformation 
{
public List ServiceHosts { get; set; }

public ServiceInformation()
{
ServiceHosts = new List();
}
}
}

I need to add strings to the List "ServiceHosts" in ServiceAPI.  The author 
placed the List inside another class, "ServiceInformation" and did not provide 
a set method, 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#: (I've been assured by 
the assembly author that this works)
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   # This is a .NET package.  
>>> Service.API.ServiceAPI.Instance.ServiceHosts.Add('1.1.1.1')
Traceback (most recent call last):
  File "", line 1, in 
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 "", line 1, in 
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 "", line 1, in 
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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/


[Python.NET] Re: Accessing a list that is a read-only property?

2020-03-03 Thread Victor “LOST” Milovanov
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 BrightonSent: Tuesday, March 3, 2020 10:42 AMTo: [email protected]: [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 ServiceHosts    {    get    {    return m_ServiceInformation.ServiceHosts;    }    }    } public class ServiceInformation {    public List ServiceHosts { get; set; } public ServiceInformation()    {    ServiceHosts = new List();    }    }}    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 "", line 1, in 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 = hostsTraceback (most recent call last):  File "", line 1, in 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 "", line 1, in 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 -- [email protected] unsubscribe send an email to [email protected]://mail.python.org/mailman3/lists/pythonnet.python.org/ ___
PythonNet mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/