create an instance of IronPython class in C#

Feb 28, 2010 at 8:34 AM

Hi,

In C# code, I am able to get a hold of a IronPython class (an instance of PythonType). Since PythonType implements IDynamicMetaObjectProvider, I figure there should be a way for me to send a 'create instance' message to it. Does anybody know how to achieve that?

My C# code looks like this:

//use ScriptRuntime to execute some python script and get back a script scope

//now retrieve the PythonType instance. Here ClassA is a Python class defined in the Python script.

dynamic ClassA = scriptScope.GetVariable("ClassA");

//How do I create an instance of the Python class? I tried 'ClassA()' and 'new ClassA()' and found that they don't work. 

Coordinator
Mar 1, 2010 at 12:17 AM

You can always use ObjectOperations to do anything that a language doesn’t natively expose (C# doesn’t have a way to dynamically create instances).  But I’m surprised “ClassA()” didn’t work – what does the Python class declaration look like?

From: rovers [mailto:notifications@codeplex.com]
Sent: Sunday, February 28, 2010 12:35 AM
To: Dino Viehland
Subject: create an instance of IronPython class in C# [dlr:203318]

From: rovers

Hi,

In C# code, I am able to get a hold of a IronPython class (an instance of PythonType). Since PythonType implements IDynamicMetaObjectProvider, I figure there should be a way for me to send a 'create instance' message to it. Does anybody know how to achieve that?

My C# code looks like this:

//use ScriptRuntime to execute some python script and get back a script scope

//now retrieve the PythonType instance. Here ClassA is a Python class defined in the Python script.

dynamic ClassA = scriptScope.GetVariable("ClassA");

//How do I create an instance of the Python class? I tried 'ClassA()' and 'new ClassA()' and found that they don't work.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Mar 1, 2010 at 6:35 AM

Hi Dino, 

Thank you for your answer. The Python class is like this:

 

class ClassA(object):
	pass

And the exception I get when I do 'ClassA()' in C# is RuntimeBinderException "Cannot invoke a non-delegate type". I'm using VS2010 Beta2 and DLR change set 42459. 

Apr 12, 2010 at 3:38 AM
Edited Apr 12, 2010 at 3:54 AM

Not sure if this helps, but I had a similar issue.

I went around the issue with :

Activator.CreateInstance(classA.__clrtype__(), classA).

classA is a RuntimeType from IronPython (your ClassA is one)

From what I could gather, each type in IronPython has a corresponding CLR type, and you can instanciate that one.

The CLR type expects the RuntimeType as a first argument.

I *think* this mimicks the __new__ protocol from python.

Can anyone more versed in the DLR check if this is the correct way to create objects in runtime from c#?

 

EDIT: actually, scratch that Activator hack, I found out about ObjectOperations:

_pythonEngine.Operations.CreateInstance(classA)

seems to be the correct way to do it