假设我解释了这个权利,并且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为空。 TestAction只是定义为:
public delegate void TestAction<T1, T2>(T1 one, T2 two);
但幸运的是,无论哪种方式,它的罚款只是做:
CameraAnimation(System.Action)
或
CameraAnimation(TestAction)
但出于某种原因,我不记得,工作时我第一次尝试...
你会列出一些*失败*尝试? 我相信我们都可以从你的失败中学习。 (http://www.codinghorror.com/blog/archives/000576.html) – Sung 2009-04-28 23:40:52
嘿,如果你坚持。我将C#代码转换为Python的方法是将C#粘贴到.py文件并删除东西,直到编译完成。这大部分工作,但不幸的是我从一个相当明显的观点分心,我应该声明一个函数并将其作为参数传递。 – 2009-04-29 07:15:35