有什么办法可以让这个场景发挥作用吗?
有一个Python脚本。通过使用 IronPython 运行以下脚本将其构建到 DLL 中:
import clr
clr.CompileModules("CompiledScript.dll", "script.py")
目标是从 C# 代码调用此 DLL 的方法。.NET反射器显示 DLL 中有一个类 -DLRCashedCode
我们感兴趣的方法是这个类的私有静态方法。
例如脚本中有一个函数:
def scriptMethod(self, text):
...
它在DLL中的表示是:
private static object scriptMethod(Closure closure1, PythonFunction $function, object self, object text)
{
...
}
Closure
and PythonFunction
是 IronPython 类(来自 Microsoft.Scripting.dll 和 IronPython.dll)。
到目前为止,一切都很好。这个方法可以被C#代码调用吗?使用反射的想法就像
Type t = typeof(DLRCachedCode);
string methodName = "scriptMethod";
MethodInfo method = t.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static);
object[] parameters = new object[] { "param1", "param2" }; // the "params problem"
method.Invoke(null, parameters);
由于设置方法的参数,似乎更难。如果它们(无论如何)正确初始化,我们可以期望该方法顺利工作吗?
有没有更好的方法从 C# 调用此方法?由于各种不同的原因,我们更喜欢将脚本构建为 .NET 程序集,而不是调用脚本本身。