我正在开发一个用 C# 编写的应用程序,它承载 IronPython。我的计划是用户将编写 python 脚本,其中包含实现预定义接口的类。
就像是:
在 C# 代码中定义接口:
public interface IPlugin
{
void DoSomething();
}
在 python 代码中实现接口。
class Plugin(IPlugin):
def DoSomething():
print "Doing stuff."
在我的应用程序中,我加载 python 脚本并执行这些脚本中的类实现的方法。
我想知道:如何获取在 python 脚本中实现指定接口的所有类的句柄?
例如:如果我要在 C# 中做所有事情,我有反射来帮助我,我可以直接加载程序集并执行以下操作:
Assembly assembly = Assembly.LoadFrom("plugin.dll");
Type[] types = assembly.GetTypes();
foreach(Type type in types)
{
foreach(Type interfaceType in type.GetInterface())
{
if(interfaceType == typeof(IPlugin))
{
// gotcha, invoke interface specific methods
}
}
}
当我处理 IronPython 脚本而不是程序集时,我该怎么做?我知道使用 ObjectOperations 和 ScriptScope,如果我知道它的名称(如此处所述),我可以获得该类的句柄,但我正在寻找更强大的东西。