[Ironpython-users] FW: CreateMethod() replacement

Jimmy Schementi jschementi at gmail.com
Mon Jul 25 23:40:11 CEST 2011

I don't have a good answer for you other than to look at what
CreateMethod did in the old codebase. The complexity is you need to
create a delegate based on the type you passed in as TDelegate, as
well as the string args. Without figuring out where in the IronPython
compiler you can get back a DynamicMethod for arbitrary Python code,
and then emit a delegate with the proper signature, it'd be quicker to
generate a randomly-named python method as a string to wrap your code
in (like generatedMethod<GUID>(*args): ...), execute it against a
ScriptScope, get the generated method name with
ScriptScope.GetProperty(generatedName), and lastly return a
Func<IEnumerable, object> which when invoked calls the generated
python function, passing in the IEnumerable as the arguments and
returning the result as an object. Let me know if you need any help.
Or maybe Dino can chime in if there is a more *direct* way to
implement CreateMethod.


On Wed, Jun 22, 2011 at 2:35 PM, Andrew Harrison <sgt_spoon at hotmail.com> wrote:
> I have recently taken over a c# project that uses IronPython 1.1. I am
> looking to update the IronPython implementation to 2.7 but having some
> issues as the api has changed dramatically. The big issue I am having at the
> moment is finding a replacement for the createMethod() function.
> The c# code looks like:
>         public static TDelegate SetupDelegate<TDelegate>(string code,
> IList<string> args) {
>             try {
>                 return interpreter.CreateMethod<TDelegate>(code, args);
>             } catch (Exception) {
>                 log.WarnFormat("Failed to compile delegate code:\n{0}",
> code);
>                 throw;
>             }
>         }
> interpreter was a PythonEngine variable, but I have changed it to a
> ScriptEngine with the new 2.7 implementation.
> If it helps at all, this system is used to create delegates for event
> handlers. There may be some code written to be activated when a button is
> clicked, which is passed into this function here then the method is created
> and returned and added to the list of event handlers.
> I have tried for a day and a half trying to get some code that worked, but
> can't seem to get anything that works.
> Any help is greatly appreciated.
> Thanks,
> Andrew.
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users at python.org
> http://mail.python.org/mailman/listinfo/ironpython-users

More information about the Ironpython-users mailing list