简体   繁体   English

集成C#,F#,IronPython和IronRuby

[英]Integration of C#, F#, IronPython and IronRuby

I was told that the assembly files made from C# and F# source is interoperable as they are compiled into .NET assembly. 我被告知,由C#和F#源组成的汇编文件可以互操作,因为它们被编译成.NET程序集。

  • Q1 : Does that mean that C# can call F# functions just like they are C# functions? Q1:这是否意味着C#可以调用F#函数就像它们是C#函数一样?
  • Q2 : How about the IronPython and IronRuby? Q2:IronPython和IronRuby怎么样? I don't see any assembly dll from the IronPython/IronRuby. 我没有看到IronPython / IronRuby中的任何程序集dll。
  • Q3 : Is there any easy way to use IronPython/IronRuby functions from C# or F#? 问题3:有没有简单的方法可以使用C#或F#中的IronPython / IronRuby函数?

Any example code would be great. 任何示例代码都会很棒。

1) Yes. 1)是的。 Using a simple example, in F#, I can invoke the main method of a C# console app: 使用一个简单的例子,在F#中,我可以调用C#控制台应用程序的main方法:

open SampleApp

SampleApp.Program.Main([| "Hello"; "World" |])

2) For the DLR, I think things are a bit different. 2)对于DLR,我觉得情况有点不同。 I believe this is how you would execute Python (for example); 我相信这就是你如何执行Python(例如);

ScriptEngine engine = Python.CreateEngine();
ScriptSource script = engine.CreateScriptFromSourceFile("myPythonScript.py");
ScriptScope scope = engine.CreateScope();

script.Execute(scope);

My syntax may be off a bit - but you should be able to get the gist. 我的语法可能有些偏差 - 但你应该能够获得要点。

3) You need to download some special DLR DLLs - and then reference them in your C# / F# application in order to be interoperable. 3)您需要下载一些特殊的DLR DLL - 然后在您的C#/ F#应用程序中引用它们以便可互操作。 They should be available from the Microsoft DLR site. 它们应该可以从Microsoft DLR站点获得。

I was told that the assembly files made from C# and F# source is interoperable as they are compiled into .NET assembly. 我被告知,由C#和F#源组成的汇编文件可以互操作,因为它们被编译成.NET程序集。

Yes, you can reference an F# assembly in a C# one. 是的,您可以在C#中引用F#程序集。 The F# objects etc will function like C# ones when you reference them. 当您引用它们时,F#对象等将像C#一样运行。

You can access IronPython/Ruby code in C# too, although it's a little different than just referencing an assembly. 您也可以在C#中访问IronPython / Ruby代码,尽管它与仅引用程序集略有不同。 Here is an example: 这是一个例子:

http://www.highoncoding.com/Articles/573_First_Look_at_the_IronRuby.aspx http://www.highoncoding.com/Articles/573_First_Look_at_the_IronRuby.aspx

Q3: Is there any easy way to use IronPython/IronRuby functions from C# or F#? 问题3:有没有简单的方法可以使用C#或F#中的IronPython / IronRuby函数?

You might have a look at Embedding IronPython into a C# application 您可以查看将IronPython嵌入到C#应用程序中

Basically, what it does (simplified code): 基本上,它做什么(简化代码):

var engine = Python.CreateEngine();
var scriptSource = engine.CreateScriptSourceFromString(@"
def foo(s):
  print s", SourceCodeKind.Statements);

var scope = engine.CreateScope();
scriptSource.Execute(scope);

//Get a reference to the function
Func<string, decimal> foo = scope.GetVariable<Func<string, decimal>>("foo");

//Execute it
Console.WriteLine(foo("a"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM