IronPython.Runtime.List
implements the following interfaces:
IList, ICollection, IList<object>, ICollection<object>, IEnumerable<object>, IEnumerable
so you can cast to one of this types an then turn into a List<string>
.
List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();
BTW, maybe you are aware of it, but you can also use .NET types in IronPython e.g.:
from System.Collections.Generic import *
class MyClass(object):
def myMethod(self):
return List[str](['a','list','of','strings'])
here myMethod
returns directly a List<string>
EDIT:
Given that you're using .net 2.0 (so no LINQ) you have two options (IMO):
1. Cast to IList<object>
and use it:
IList<object> result = (IList<object>)op.Invoke(method);
PROs: no loop required, you will use the same object instance returned by the python script.
CONs: no type safety (you will be like in python, so you can add also a non-string to the list)
2. Convert to a List<string>
/IList<string>
:
IList<object> originalResult = (IList<object>)op.Invoke(method);
List<string> typeSafeResult = new List<string>();
foreach(object element in originalResult)
{
typeSafeResult.Add((string)element);
}
PROs: type safe list (you can add only strings).
CONs: it requires a loop, and the converted list is a new instance (not the same returned by the script)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…