如何将 lambda 表达式从 IronPython 脚本传递到 C# 构造函数?

发布于 2024-07-18 08:24:53 字数 434 浏览 3 评论 0原文

我正在将 IronPython 脚本引擎集成到我的 C# 光线跟踪器中,尽管我对 Python 完全陌生,但到目前为止,这还是一件轻而易举的事。 不过,有一件特别的事情我需要帮助。 我有一个 C# 类,它定义了这样的构造函数:

public CameraAnimation(Action<Camera, float> animation)

在 C# 中,我会像这样实例化它:

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

我不太清楚如何在 IronPython 中为 Action 对象进行类似的赋值,那么 Python 语法会是什么样子呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-07-25 08:24:53

假设我对此解释正确,并且 Action 是通用委托,则以下内容有效(包括我使用的存根)。

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);
    }
    }
 }

我更正了上面的内容以使用 System.Action,并且它不再需要显式反射。 不过这有点奇怪。 由于某种原因,我可以构造一个用户创建的委托,例如:

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

但不能使用 System.Action 这样做。 例如,

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

explicitSystemAction 为 null。 TestAction 只是定义为:

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

但幸运的是,无论哪种方式,都可以这样做:

CameraAnimation(System.Action) 

或者

CameraAnimation(TestAction)

尽管出于某种原因,我不记得我第一次尝试时的工作...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文