Page 1 of 1

Using a .NET DLL with a script-able dynamic language

Posted: October 25th, 2010, 11:42 pm
by Hansi
I have a C# .NET based API defined in a DLL that exposes a set of services and classes that allows connections to an existing application/server. I want to be able to load the DLL into a dynamic language that offers easy script-ability for someone used to Python. I've only used Python for simple web service stuff so I'm not really familiar with it but it looks like it's DLL support is more like a COM function only based thing with support only for basic types. What I want to do is gain complete access to the services and classes inside the DLL so I can define objects, interact with them etc and have it script-able and able to run with Windows task scheduler.It looks like IronPython may be the best choice here but I have no experience with it so I wanted to check if anyone had done something similar and if you guys had used IronPython or something else? Am I asking for too much? Should I refactor the DLL to be exposed as a CLI/C++ instance with the C# code wrapped and make it completely function based?

Using a .NET DLL with a script-able dynamic language

Posted: October 26th, 2010, 8:09 am
by kziemski
Ironpython works and can easily be used for this.another option Is powershell

Using a .NET DLL with a script-able dynamic language

Posted: October 26th, 2010, 9:01 am
by Hansi
I couldn't import the dll so I just tried this simple one:using System;namespace ClassLibrary1{ public class Class1 { public static int AddOne(int n) { return ++n; } }}Just getting this generic error in both cases, any ideas?

Using a .NET DLL with a script-able dynamic language

Posted: October 26th, 2010, 11:49 am
by CurtHagenlocher
The "current directory" isn't part of the search path for AddReference. You could sys.path.append the directory or you could use clr.AddReferenceToFileAndPath and give a full path name.

Using a .NET DLL with a script-able dynamic language

Posted: October 26th, 2010, 11:19 pm
by Hansi
That was the first thing I tried but it didn't work with that either. Also IronPython docs say that the CWD is always part of sys.path.

Using a .NET DLL with a script-able dynamic language

Posted: October 27th, 2010, 12:39 pm
by CurtHagenlocher
Hmmm... worked for me.PS C:\> type test\x.csusing System;namespace ClassLibrary1{public class Class1{public static int AddOne(int n){return ++n;}}}PS C:\> & 'C:\Program Files (x86)\IronPython 2.6 for .NET 4.0\ipy.exe'IronPython 2.6.2 (2.6.10920.0) on .NET 4.0.30319.1Type "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.chdir('c:/test')>>> os.listdir(os.curdir)['x.cs', 'x.dll', 'x.pdb']>>> import clr>>> clr.AddReference('x.dll')Traceback (most recent call last): File "<stdin>", line 1, in <module>IOError: System.IO.IOException: Could not add reference to assembly x.dll at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean& shouldOptimize) at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`2.Call1(CallSite site, CodeContext context, TFuncType func, T0 arg0) at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2) at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`2.Call1(CallSite site, CodeContext context, TFuncType func, T0 arg0) at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) at IronPython.Compiler.PythonScriptCode.Run(Scope scope) at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction>b__0()>>> import sys>>> sys.path.append('c:/test')>>> clr.AddReference('x.dll')>>> from ClassLibrary1 import Class1>>> Class1.AddOne(3)4>>>

Using a .NET DLL with a script-able dynamic language

Posted: October 27th, 2010, 3:39 pm
by CurtHagenlocher
QuoteOriginally posted by: HansiThat was the first thing I tried but it didn't work with that either. Also IronPython docs say that the CWD is always part of sys.path.Dino says "I don't think [this behavior] is intentional. We actually have a bug to remove . from the sys.path when we're not in interactive mode so it'd probably be safe to make this work."

Using a .NET DLL with a script-able dynamic language

Posted: October 27th, 2010, 9:03 pm
by Hansi
Thanks for that, tried adding the path to sys.path again and it works now. I must have made a mistake last time I tried it.