Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4042920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:03:52+00:00 2026-05-20T13:03:52+00:00

I want to pass a list of strings from IronPython 2.6 for .NET 2.0

  • 0

I want to pass a list of strings from IronPython 2.6 for .NET 2.0 to a C# program (I’m using .NET 2.0 because I’m working with an api that runs off of DLLs built on 2.0). But I’m not sure how to cast it as it comes back from the ScriptEngine.

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptEngine engine = Python.CreateEngine();
            ScriptSource source = engine.CreateScriptSourceFromFile("C:\\File\\Path\\To\\my\\script.py");
            ScriptScope scope = engine.CreateScope();

            ObjectOperations op = engine.Operations;

            source.Execute(scope); // class object created
            object classObject = scope.GetVariable("MyClass"); // get the class object
            object instance = op.Invoke(classObject); // create the instance
            object method = op.GetMember(instance, "myMethod"); // get a method
            List<string> result = (List<string>)op.Invoke(method); // call the method and get result
            Console.WriteLine(result.ToString());
            Console.Read();
        }
    }
}

my python code has a class with a method that returns a python list of strings:

class MyClass(object):
    def myMethod(self):
        return ['a','list','of','strings']

I get this error:

Unable to cast object of type 'IronPython.Runtime.List' to type 'System.Collections.Generic.List`1[System.String]'.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:03:52+00:00Added an answer on May 20, 2026 at 1:03 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to pass back from a user control a list of strings that
I have a list of UTF-8 strings that I want to sort using Enumerable.OrderBy
I need to pass 4 arguments (3 strings and one comma separated list) from
I want to pass a python list of strings to a C function expecting
I want to pass a list of strings to a procedure and use it
I want to pass a property list of a class to a function. with
I want to pass a few variables to another page. Currently I'm using response.redirect
I want to pass around some values in an array that will always be
I want to pass a custom made vector object containing data from background service
I want a List<string> containing all the 'selected' series codes from this DataGridView: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.