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 973807
In Process

The Archive Base Latest Questions

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

I want to create an instance of an IronPython class from C#, but my

  • 0

I want to create an instance of an IronPython class from C#, but my current attempts all seem to have failed.

This is my current code:

ConstructorInfo[] ci = type.GetConstructors();

foreach (ConstructorInfo t in from t in ci
                              where t.GetParameters().Length == 1
                              select t)
{
    PythonType pytype = DynamicHelpers.GetPythonTypeFromType(type);
    object[] consparams = new object[1];
    consparams[0] = pytype;
    _objects[type] = t.Invoke(consparams);
    pytype.__init__(_objects[type]);
    break;
}

I am able to get the created instance of the object from calling t.Invoke(consparams), but the __init__ method doesn’t seem to be called, and thus all the properties that I set from my Python script aren’t used. Even with the explicit pytype.__init__ call, the constructed object still doesn’t seem to be initialised.

Using ScriptEngine.Operations.CreateInstance doesn’t seem to work, either.

I’m using .NET 4.0 with IronPython 2.6 for .NET 4.0.

EDIT: Small clarification on how I’m intending to do this:

In C#, I have a class as follows:

public static class Foo
{
    public static object Instantiate(Type type)
    {
        // do the instantiation here
    }
}

And in Python, the following code:

class MyClass(object):
    def __init__(self):
        print "this should be called"

Foo.Instantiate(MyClass)

The __init__ method never seems to be called.

  • 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-16T03:20:04+00:00Added an answer on May 16, 2026 at 3:20 am

    This code works with IronPython 2.6.1

        static void Main(string[] args)
        {
            const string script = @"
    class A(object) :
        def __init__(self) :
            self.a = 100
    
    class B(object) : 
        def __init__(self, a, v) : 
            self.a = a
            self.v = v
        def run(self) :
            return self.a.a + self.v
    ";
    
            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            engine.Execute(script, scope);
    
            var typeA = scope.GetVariable("A");
            var typeB = scope.GetVariable("B");
            var a = engine.Operations.CreateInstance(typeA); 
            var b = engine.Operations.CreateInstance(typeB, a, 20);
            Console.WriteLine(b.run()); // 120
        }
    

    EDITED according to clarified question

        class Program
        {
            static void Main(string[] args)
            {
                var engine = Python.CreateEngine();
                var scriptScope = engine.CreateScope();
    
                var foo = new Foo(engine);
    
                scriptScope.SetVariable("Foo", foo);
                const string script = @"
    class MyClass(object):
        def __init__(self):
            print ""this should be called""
    
    Foo.Create(MyClass)
    ";
                var v = engine.Execute(script, scriptScope);
            }
        }
    
    public  class Foo
    {
        private readonly ScriptEngine engine;
    
        public Foo(ScriptEngine engine)
        {
            this.engine = engine;
        }
    
        public  object Create(object t)
        {
            return engine.Operations.CreateInstance(t);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.