Access host class from IronPython script - c#

How do I access a C# class from IronPython script?
C#:
public class MyClass
{
}
public enum MyEnum
{
One, Two
}
var engine = Python.CreateEngine(options);
var scope = engine.CreateScope();
scope.SetVariable("t", new MyClass());
var src = engine.CreateScriptSourceFromFile(...);
src.Execute(scope);
IronPython script:
class_name = type(t).__name__ # MyClass
class_module = type(t).__module__ # __builtin__
# So this supposed to work ...
mc = MyClass() # ???
me = MyEnum.One # ???
# ... but it doesn't
UPDATE
I need to import classes defined in a hosting assembly.

You've set t to an instance of MyClass, but you're trying to use it as if it were the class itself.
You'll need to either import MyClass from within your IronPython script, or inject some sort of factory method (since classes aren't first-class objects in C#, you can't pass in MyClass directly). Alternatively, you could pass in typeof(MyClass) and use System.Activator.CreateInstance(theMyClassTypeObject) to new up an instance.
Since you also need to access MyEnum (note you're using it in your script without any reference to where it might come from), I suggest just using imports:
import clr
clr.AddReference('YourAssemblyName')
from YourAssemblyName.WhateverNamespace import MyClass, MyEnum
# Now these should work, since the objects have been properly imported
mc = MyClass()
me = MyEnum.One
You might have to play around with the script source type (I think File works best) and the script execution path to get the clr.AddReference() call to succeed.

Related

How to properly setup CodeContext of IronPython to directly invoke IO from C#?

I am trying to directly invoke IronPython's built-in modules from C#. It looks like I'm missing some important initialization, that I can't find anywhere in the code.
Here's what I do:
namespace py.consoleio
{
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using Microsoft.Scripting.Runtime;
public static class consoleio
{
public static string name;
static void Main()
{
var setup = new ScriptRuntimeSetup();
setup.LanguageSetups.Add(
IronPython.Hosting.Python.CreateLanguageSetup(null));
var dlrRuntime = new ScriptRuntime(setup);
var scriptDomainManager = HostingHelpers.GetDomainManager(dlrRuntime);
var pythonContext = new PythonContext(scriptDomainManager, null);
var context = new CodeContext(new PythonDictionary(), new ModuleContext(new PythonDictionary(), DefaultContext.DefaultPythonContext));
name = IronPython.Modules.Builtin.input(context, "What is your name?\n");
IronPython.Modules.Builtin.print(context, "Hi, %s.", consoleio.name);
System.GC.KeepAlive(pythonContext);
}
}
}
That properly outputs "What is your name?", but then crashes trying to decode input: unknown encoding: cp437.
Now I've already found, that encodings are initialized in Src/StdLib/Lib/encodings/init.py
I can't find how it gets to loading this module in a normal IronPython run (e.g. a console host), so I can't reproduce it in C# program.
My goal here is to invoke IronPython functions without dynamic dispatch.
UPD. Now I also tried to do this:
var engine = Python.CreateEngine();
this.ScriptDomainManager = HostingHelpers.GetDomainManager(engine.Runtime);
to the same result
Figured that one out: encodings module is implemented in Python in IronPython (core modules are in C#). It always worked with IronPythonConsole project, because it implicitly adds IronPython source for standard libraries to Python path. I just had to explicitly specify path like this:
var options = new Dictionary<string, object> { ["SearchPaths"] = path };
var engine = Python.CreateEngine(options);

Roslyn scripting with enforced script interface

I have simple IScript interface. And I want enforce that all scripts implement it.
public interface IScript<T>
{
T Execute(object[] args);
}
I want to use Roslyn scripting API to achive this. Something like this is possible with CSScript (see Interface Alignment).
var code = #"
using System;
using My.Namespace.With.IScript;
public class Script : IScript<string>
{
public string Execute()
{
return ""Hello from script!"";
}
}
";
var script = CSharpScript.Create(code, ScriptOptions.Default); // + Load all assemblies and references
script.WithInterface(typeof(IScript<string>)); // I need something like this, to enforce interface
script.Compile();
string result = script.Execute(); // and then execute script
Console.WriteLine(result); // print "Hello from script!"
Type safety is a static thing enforced a compile time (of your application). Creating and running a CSharpScript is done at runtime. So you cannot enforce type safety at runtime.
Maybe CSharpScript is not the right way to go. By using this SO answer,
You can compile a piece of C# code into memory and generate assembly bytes with Roslyn.
You would then change the line
object obj = Activator.CreateInstance(type);
to
IScript<string> obj = Activator.CreateInstance(type) as IScript<string>;
if (obj != null) {
obj.Execute(args);
}

Implicitly creating an instance of a class that is located in a dependent dll

Let's say you are developing a third-party DLL called "ThirdParty.dll" which has a class called JustTryToInstantiateMe. Let's say your customer has an EXE that has a reference to "ThirdParty.dll" or maybe the EXE doesn't have a reference to "ThirdParty.dll" but the EXE can find "ThirdParty.dll" in the PATH if somehow requested. You have no control over the EXE as you are the vendor for the "Thirdparty.dll" and you can only modify what the DLL does. Your customer, the one writing the EXE, agrees to either make a reference to your DLL or put your DLL in the PATH. Nothing more.
Is there a way to instantiate JustTryToInstantiateMe when running the EXE without explicitly using new or invoking a static/Shared method/field/property?
Maybe just using the static/Shared constructor somehow? Maybe on AssemblyLoad() event? Maybe using the registry? a startup process? Looking for ideas...
The nearest way I know how to do this is to dynamically load the assembly and then invoke a method from an instance of a class that you create.
Here is a template in c# for the code:
//add using for system.reflection
String className = "[NAME OF CLASS WITH FULL NAMESPACE GOES HERE]";
String methodName = "[METHOD NAME GOES HERE]"
String dllPath = "[FILE PATH FOR DLL GOES HERE]";
Assembly assembly = Assembly.LoadFile(dllPath);
Type type = assembly.GetType(className);
MethodInfo method = type.GetMethod(methodName);
object context = Activator.CreateInstance(type);
//optionally set up parameters here
object[] parameters = new object[0];
String result = (String) method.Invoke(context, parameters);
For example, I could create a class like so (I know, it's VB, but what follows after is C#.
Public Class my_class
Private hello = "hello world"
Public Function gethello()
Return hello
End Function
End Class
Then after it is compiled, I can use the following code in C# to do what I think it is you are asking:
//using system.reflection
String className = "mytestlibrary.my_class";
String dllPath = "...mytestlibrary.dll";
String methodName = "gethello";
Assembly assembly = Assembly.LoadFile(dllPath);
Type type = assembly.GetType(className);
MethodInfo method = type.GetMethod(methodName);
object context = Activator.CreateInstance(type);
object[] parameters = new object[0];
String result = (String) method.Invoke(context, parameters);
//result is "hello world"
Now, if you want to do the same, but with an already compiled EXE file, then the code is hardly different, but you will need to create a desktop/console application to shell the other executable and properly namespace your DLL. So in YOUR application, add the following code to grab an instance of a class from the DLL referenced inside the EXE.
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
String className = "My_Class";
String ExePath = "[PATH TO EXE]";
String methodName = "gethello";
Assembly assembly = Assembly.LoadFile(ExePath);
Type type = assembly.GetType(className);
MethodInfo method = type.GetMethod(methodName);
object context = Activator.CreateInstance(type);
object[] parameters = new object[0];
String result = (String) method.Invoke(context, parameters);
It is also possible for you to dynamically load assemblies into the EXE, but that is a bit more tricky, so I will leave it at this for now unless you really need to dynamically load your DLL into the EXE.
From research I found out that the simple answer to this question is that this is not possible. The short answer is that in order for a class in a DLL to be loaded the class needs to be referenced by something. If a DLL were able to self-load itself by virtue of JUST being in the PATH this would bring about all kinds of security issues.

Calling a function with named arguments in a hosted application

So I am hosting IronPython in my C# application. IronPhyton is used to implement a DSL for users. The DSL syntax should be something like this:
Ping(Message = "testOne1")
The hosting code looks like:
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
Action<string> ping = (message) => Console.WriteLine(message.ToString());
scope.SetVariable("Ping", ping);
var script = #"
Ping(Message = ""testOne1"")
";
engine.Execute(script, scope);
But this does not work because Action<string> does not keep name of the argument. Calling it without the parameter name works as expected:
Ping("testOne1")
How do I store a function and call it with named arguments?
To use named arguments you'll have to define the method statically. For example, I'll just put all DSL operations into an Operations static class.
public static class Operations {
public static void Ping(string Message) {
Console.WriteLine(Message);
}
}
Then named arguments will work:
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
// Load the assembly where the operations are defined.
engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly());
// Import the operations modules, settings their names as desired.
engine.Execute(#"
from Operations import Ping
", scope);
// Now named arguments will work...
var script = #"
Ping(Message = ""Ping!"")
";
engine.Execute(script, scope);
Now if I could give you some advise; I'd prefer to implement the actual Python API in Python, and have that call back into my .NET code as needed. For example, instead of having the "operations" defined in C#, you'd have an Operations.py file which defines your Python DSL:
# Get access to your .NET API
import clr
clr.AddReference("MyAPI")
import MyAPI
# Define the Ping call to call into your .NET API
def Ping(Message):
MyAPI.Ping(Message)
And your hosting code doesn't need to change at all.
Both are valid solutions, but the last one lets you iterate on your DSL easily.
Good luck!
The name of the parameter is defined by the name provided in the delegate type. In the case of Action<T>, the parameter name is obj.
public delegate void Action<in T>(
T obj
)
obj should work for you. Are you sure it isn't working? It works for me.
In an IronPython project I have a library:
namespace TestLibrary
{
public static class Test
{
public static readonly Action<string> WriteLine =
msg => Console.WriteLine(msg);
// the same works if I do this instead
//public static readonly Action<string> WriteLine = Console.WriteLine;
}
}
And this works:
from TestLibrary import Test
#Test.WriteLine(msg='foo') # error
Test.WriteLine(obj='foo') # works
Hosted, same deal:
var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
Action<string> writeLine = msg => Console.WriteLine(msg);
// or even
//Action<string> writeLine = Console.WriteLine;
scope.writeLine = writeLine;
//engine.Execute("writeLine(msg='foo')", scope); // error
engine.Execute("writeLine(obj='foo')", scope); // works

C#, IronPython - import(?) from a non-static class

I have a non-static C# class with some instance methods, which I need to call from IronPython scripts. Currently I'm doing it this way:
scope.SetVariable("class", instanceOfClass);
in C# code and
class.SomeMethod(args)
in script.
What I want is being able to call this class methods without adding class. each time in the script. Each script has its own instance of the class, and only one instance is used in one script.
If this class was static, the solution would be from ClassName import *, but as I know there is no similar construction for non-static classes.
How can this be done? I have some ideas (such as using reflection, or adding class. to each call in Python source programmatically), but they are overcomplicated and may be even not possible to implement.
UPD:
Problem solved by using such python code (before actual script):
def Method1(arg1): # for simple method
class.Method1(arg1)
def Method2(arg = 123): # for default values
class.Method2(arg)
def Method3(*args): # for params
class.Method3(args)
# so on
from ClassName import * is actually from namespace import type. This statement makes the type avaiable for use via the type name in Python. It makes no difference if the class is static or not. Consider this sample code - Environment being the static class.
import clr
from System import Environment
print Environment.CurrentDirectory
To solve your problem, inject a delegate to the class function into your ScriptScope, rather than the class itself.
Sample class
public class Foo {
public string GetMyString(string input) {
return input;
}
}
Usage
private static void Main(string[] args) {
ScriptEngine engine = Python.CreateEngine();
string script = "x = GetMyString('value')";
Foo foo = new Foo();
ScriptSource scriptSource = engine.CreateScriptSourceFromString(script);
ScriptScope scope = engine.CreateScope();
scope.SetVariable("GetMyString", new Func<string, string>(foo.GetMyString));
scriptSource.Execute(scope);
string output = scope.GetVariable<string>("x");
Console.WriteLine(output);
}
prints
value

Categories

Resources