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:
我正在将一个IronPython scritping引擎集成到我的C#raytracer中,到目前为止,尽管我对Python完全不熟悉,但它仍然是轻而易举的。但是,有一件特别的事情,我需要帮助。我有一个C#类,它定义了一个这样的构造函数:
public CameraAnimation(Action<Camera, float> animation)
In C#, I would instantiate this like so:
在C#中,我会像这样实例化:
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?
我无法弄清楚如何为IronPython中的Action对象做出类似的赋值,那么Python语法将如何呈现呢?
1 个解决方案
#1
2
Assuming I interpreted this right, and Action is a generic delegate, the below works (the stubs I used are included).
假设我解释了这一点,并且Action是一个通用委托,下面的工作(我使用的存根包括在内)。
Python:
蟒蛇:
import clr
clr.AddReference("IronPythonDelegates")
import IronPythonDelegates
def camActionPy(camera, time):
print "Camera: " + str(camera) + ", time: " + str(time)
IronPythonDelegates.CameraAnimation(camActionPy);
CSharp:
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:
我更正了上面的内容以使用System.Action,它不再需要显式反射。虽然这有点奇怪。出于某种原因,我可以构建一个用户创建的委托,如:
explicitTestAction = IronPythonDelegates.TestAction[IronPythonDelegates.Camera, System.Single](camActionPy);
IronPythonDelegates.CameraAnimation(explicitTestAction);
but could not do so with System.Action. E.g. with
但是使用System.Action无法做到这一点。例如。同
explicitSystemAction = System.Action[IronPythonDelegates.Camera, System.Single](camActionPy)
IronPythonDelegates.CameraAnimation(explicitSystemAction);
explicitSystemAction is null. TestAction was just defined as:
explicitSystemAction为null。 TestAction刚刚被定义为:
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...
虽然出于某种原因,我不记得我第一次尝试时的工作......
#1
2
Assuming I interpreted this right, and Action is a generic delegate, the below works (the stubs I used are included).
假设我解释了这一点,并且Action是一个通用委托,下面的工作(我使用的存根包括在内)。
Python:
蟒蛇:
import clr
clr.AddReference("IronPythonDelegates")
import IronPythonDelegates
def camActionPy(camera, time):
print "Camera: " + str(camera) + ", time: " + str(time)
IronPythonDelegates.CameraAnimation(camActionPy);
CSharp:
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:
我更正了上面的内容以使用System.Action,它不再需要显式反射。虽然这有点奇怪。出于某种原因,我可以构建一个用户创建的委托,如:
explicitTestAction = IronPythonDelegates.TestAction[IronPythonDelegates.Camera, System.Single](camActionPy);
IronPythonDelegates.CameraAnimation(explicitTestAction);
but could not do so with System.Action. E.g. with
但是使用System.Action无法做到这一点。例如。同
explicitSystemAction = System.Action[IronPythonDelegates.Camera, System.Single](camActionPy)
IronPythonDelegates.CameraAnimation(explicitSystemAction);
explicitSystemAction is null. TestAction was just defined as:
explicitSystemAction为null。 TestAction刚刚被定义为:
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...
虽然出于某种原因,我不记得我第一次尝试时的工作......