[Ironpython-users] How to bind a System.Type to a variable using scope.SetVariable?

Markus Schaber m.schaber at codesys.com
Tue Mar 29 03:03:42 EDT 2016


Hi, Buckle,
I’m trying to do some guesswork, as I could not find a clear question in the first part.
I guess you’re using a “hosted” environment, where the IronPython interpreter is hosted in a C# / .NET application.
There’s the method DynamicHelpers.GetPythonTypeFromType(Type dotNetType) which creates a python class object for a .NET type.
We successfully used this to provide types (including enums) to IronPython scripts. You can either inject them directly into the script scope, or create an artificial module using ScriptEngine.CreateModule(string name) and populate its namespace with mymodule.SetVariable(name, value), which the script can then “import”.
Best regards

Markus Schaber

CODESYS® a trademark of 3S-Smart Software Solutions GmbH

Inspiring Automation Solutions
________________________________
3S-Smart Software Solutions GmbH
Dipl.-Inf. Markus Schaber | Product Development Core Technology
Memminger Str. 151 | 87439 Kempten | Germany
Tel. +49-831-54031-979 | Fax +49-831-54031-50

E-Mail: m.schaber at codesys.com<mailto:m.schaber at codesys.com> | Web: codesys.com<http://www.codesys.com> | CODESYS store: store.codesys.com<http://store.codesys.com>
CODESYS forum: forum.codesys.com<http://forum.codesys.com>

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner | Trade register: Kempten HRB 6186 | Tax ID No.: DE 167014915
________________________________
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received
this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure
or distribution of the material in this e-mail is strictly forbidden.
From: Ironpython-users [mailto:ironpython-users-bounces+m.schaber=codesys.com at python.org] On Behalf Of buckle2000
Sent: Tuesday, March 22, 2016 5:42 PM
To: ironpython-users
Subject: [Ironpython-users] How to bind a System.Type to a variable using scope.SetVariable?

Hello everyone!
I want to bind a class in C# (or a System.Type) to a variable in IronPython using scope.SetVariable.
For example,
import System
math = System.Math
math.Sin(1) # this works
type(math) # IronPython.Runtime.Types.PythonType
But, I do not want to use "import xxx" due to security problems.
I want the "math" class, in this example, can be use instantly.

When I tried to do something like this in C#:
scope.SetVariable("math",typeof(System.Math));
then in python code
type(math) # Also IronPython.Runtime.Types.PythonType
See the attached source file for detailed info.

Something may be useful:
see IronPython.Modules.Builtin, which is also a static class like System.Math.
seems like IronPython binded it with "__builtins__" in python successfully, but I don't know how.

Another question: is there an easy way to disable users from importing modules like clr without using Code Access?
Also, how to bind a namespace in C# code, to a ironpython object, using IronPython C# assembly (so the "binding" code must be written in C#(.NET) code)?


2016-03-19
________________________________
buckle2000
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20160329/94ee8c1e/attachment.html>


More information about the Ironpython-users mailing list