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