如何将 lambda 表达式从 IronPython 脚本传递到 C# 构造函数?
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设我对此解释正确,并且 Action 是通用委托,则以下内容有效(包括我使用的存根)。
Python:
CSharp:
我更正了上面的内容以使用 System.Action,并且它不再需要显式反射。 不过这有点奇怪。 由于某种原因,我可以构造一个用户创建的委托,例如:
但不能使用 System.Action 这样做。 例如,
explicitSystemAction 为 null。 TestAction 只是定义为:
但幸运的是,无论哪种方式,都可以这样做:
或者
尽管出于某种原因,我不记得我第一次尝试时的工作...
Assuming I interpreted this right, and Action is a generic delegate, the below works (the stubs I used are included).
Python:
CSharp:
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:
but could not do so with System.Action. E.g. with
explicitSystemAction is null. TestAction was just defined as:
But luckily either way it's fine to just do:
or
though for some reason I don't remember that working when I first tried...