[IronPython] IP 2.7b1 - Accessing C# variables from Python

Steve Dower s.j.dower at gmail.com
Wed Jan 5 23:45:52 CET 2011


If you're using C# 4.0 (comes with VS 2010/.NET 4) you can use the
dynamic type to simplify this further:

dynamic scope = engine.CreateScope();
scope.hash = new Dictionary<string, string>();
scope.hash["key"] = "value";

On Thu, Jan 6, 2011 at 07:11, Ron Lindsey <ron at emirot.com> wrote:
> Pascal,
>
> Well, I feel silly now. I was making it out to be too hard. Your example set
> me on the right path. Thank you!
>
> Awesome user group!
>
> Thanks, Ron Lindsey
>
> On 1/5/2011 1:45 PM, Pascal Normandin wrote:
>>
>> Hello,
>>
>> Here is a simple way to call into C# variables from IronPython using a
>> scope.
>>
>> var engine = IronPython.Hosting.Python.CreateEngine();
>> var scope = engine.CreateScope();
>> var dict = new Dictionary<string, string>();
>> dict["key"] = "value";
>> scope.SetVariable("hash", dict);
>>
>> // Calling dict from IP
>> engine.Execute("print hash['key']", scope);
>>
>>
>> Pascal
>> -----Original Message-----
>> From: users-bounces at lists.ironpython.com
>> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ron Lindsey
>> Sent: January-05-11 2:25 PM
>> To: Daniel Jennings
>> Cc: Discussion of IronPython
>> Subject: Re: [IronPython] IP 2.7b1 - Accessing C# variables from Python
>>
>> Daniel,
>>
>> Thanks for the reply. To your question, No. The real problem here is how
>> do i get access to the variables in c# from ironpython.
>>
>> Thanks, Ron Lindsey
>>
>> On 1/5/2011 1:22 PM, Daniel Jennings wrote:
>>>
>>> Have you tried just treating it like a regular Python dictionary? I
>>
>> thought that's all I had to do, though I work with Dictionary<K,V>, not
>> Hashtable objects.
>>>
>>> -----Original Message-----
>>> From: users-bounces at lists.ironpython.com
>>
>> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ron Lindsey
>>>
>>> Sent: Wednesday, January 05, 2011 11:21 AM
>>> To: users at lists.ironpython.com
>>> Subject: [IronPython] IP 2.7b1 - Accessing C# variables from Python
>>>
>>> I have written an OpenGL 3D modeling program in C#/OpenTK. In the
>>> program, I store all the objects (box, sphere, tube, etc) the user
>>> created into a hashtable. Each object stored is of type Box, Sphere
>>> etc...
>>>
>>> I am embedding IronPython to give the user a way to animate their
>>> models. So in IronPython, the user needs to have access to the objects
>>> in the hashtable to get/set properties of the different objects.
>>>
>>> Once the script is written, they then will run it. as the script
>>> executes, it will control the objects in the hashtable, which in turn
>>> will move the 3D models in the screen.
>>>
>>> I have looked/googled/read but have not found out a way to access a C#
>>> hashtable and the objects stored with in it.
>>>
>>> Any ideas?
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>



More information about the Ironpython-users mailing list