5

I'm integrating an IronPython scritping engine into my C# raytracer which, so far, has been a breeze even though I'm completely new to Python. There is one particular thing, though, that I need help with. I have a C# class which defines a constructor like this:

public CameraAnimation(Action<Camera, float> animation)

In C#, I would instantiate this like so:

var camAnimation = new CameraAnimation((camera, time) => camera.Position += new Vector(1, 0, 0));

I can't quite figure out how to make a similar assignment for the Action object in IronPython, so how would the Python syntax look?

4
  • 2
    Would you list some of the failure attempts? I am sure that we can all learn from your failure. (codinghorror.com/blog/archives/000576.html) Commented Apr 28, 2009 at 23:40
  • Heh, if you insist. My approach to converting C# code to Python was to paste the C# into a .py file and remove stuff until it compiled. This mostly worked but unfortunately distracted me from the rather obvious point that I should just declare a function and pass it as a parameter. Commented Apr 29, 2009 at 7:15
  • Don't worry. I did the same thing. :) Commented Apr 29, 2009 at 8:44
  • 1
    Current URI for @Sung's link: blog.codinghorror.com/fail-early-fail-often Commented Mar 7, 2018 at 20:01

1 Answer 1

2

Assuming I interpreted this right, and Action is a generic delegate, the below works (the stubs I used are included).

Python:

import clr
clr.AddReference("IronPythonDelegates")

import IronPythonDelegates

def camActionPy(camera, time):
  print "Camera: " + str(camera) + ", time: " + str(time)

IronPythonDelegates.CameraAnimation(camActionPy);

CSharp:

namespace IronPythonDelegates
{
    public class Camera{}

    public class CameraAnimation
    {
    private System.Action<Camera, float> animation;

    public CameraAnimation(System.Action<Camera, float> animation)
    {
        this.animation = animation;
        this.animation(new Camera(), 1.5f);
    }
    }
 }

I corrected the above to use System.Action, and it no longer requires explicit reflection. It's a bit weird though. For some reason, I could construct a user-created delegate like:

explicitTestAction = IronPythonDelegates.TestAction[IronPythonDelegates.Camera, System.Single](camActionPy);
IronPythonDelegates.CameraAnimation(explicitTestAction);

but could not do so with System.Action. E.g. with

explicitSystemAction = System.Action[IronPythonDelegates.Camera, System.Single](camActionPy)
IronPythonDelegates.CameraAnimation(explicitSystemAction);

explicitSystemAction is null. TestAction was just defined as:

public delegate void TestAction<T1, T2>(T1 one, T2 two);

But luckily either way it's fine to just do:

CameraAnimation(System.Action) 

or

CameraAnimation(TestAction)

though for some reason I don't remember that working when I first tried...

Sign up to request clarification or add additional context in comments.

3 Comments

Note, I found System.Action, but unfortunately changing it to use that inexplicably broke the code... Looking at it now.
Did you add a reference to System.Core.dll? System.Action<T> is defined in mscorlib but System.Action<T1,T2> is defined in System.Core.
Yes, I have a reference to System.Core. As noted, I have it working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.