是否可以将IronPython类声明为"Export",从而将它们添加到主机C#应用程序可以导入的MEF目录中?
我真的找不到任何具体的例子,只是推测.
以下是我手动加载实现.NET接口的Python类的方法:
https://github.com/versionone/VersionOne.SDK.Experimental
我希望能够在python类上放置类似于在C#中执行它的方式.(或等同的东西)
有没人试过这个?
谢谢,乔希
对于那些感兴趣的人,我在GitHub上发现了一个已完成此任务的项目,但有点与项目相关.经过作者的批准,我为它创建了一个新的repo,IronPythonMef和一个NuGet包.
在GitHub的这个帖子中还有另外的讨论.
这是一个如何工作的例子:
首先,在C#中声明一个接口:
namespace IronPythonMef.Tests.Example.Operations
{
public interface IOperation
{
object Execute(params object[] args);
string Name { get; }
string Usage { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
在C#中导出该接口的实现:
[Export(typeof(IOperation))]
public class Power : IOperation
{
public object Execute(params object[] args)
{
if (args.Length < 2)
{
throw new ArgumentException(Usage, "args");
}
var x = Convert.ToDouble(args[0]);
var y = Convert.ToDouble(args[1]);
return Math.Pow(x, y);
}
public string Name
{
get { return "pow"; }
}
public string Usage
{
get { return "pow n, y -- calculates n to the y power"; }
}
}
Run Code Online (Sandbox Code Playgroud)
并且,在IronPython中实现了IOperation:
@export(IOperation)
class Fibonacci(IOperation):
def Execute(self, n):
n = int(n)
if n == 0:
return 0
elif n == 1:
return 1
else:
return self.Execute(n-1) + self.Execute(n-2)
@property
def Name(self):
return "fib"
@property
def Usage(self):
return "fib n -- calculates the nth Fibonacci number"
Run Code Online (Sandbox Code Playgroud)
这是一个类的测试用例,它从C#和IronPython导入这些操作并执行它们:
[TestFixture]
public class MathWizardTests
{
[Test]
public void runs_script_with_operations_from_both_csharp_and_python()
{
var mathWiz = new MathWizard();
new CompositionHelper().ComposeWithTypesExportedFromPythonAndCSharp(
mathWiz,
"Operations.Python.py",
typeof(IOperation));
const string mathScript =
@"fib 6
fac 6
abs -99
pow 2 4
";
var results = mathWiz.ExecuteScript(mathScript).ToList();
Assert.AreEqual(8, results[0]);
Assert.AreEqual(720, results[1]);
Assert.AreEqual(99f, results[2]);
Assert.AreEqual(16m, results[3]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
523 次 |
| 最近记录: |