Integration of C#, F#, IronPython and IronRuby

半城伤御伤魂 提交于 2019-12-06 02:43:56

问题


I was told that the assembly files made from C# and F# source is interoperable as they are compiled into .NET assembly.

  • Q1 : Does that mean that C# can call F# functions just like they are C# functions?
  • Q2 : How about the IronPython and IronRuby? I don't see any assembly dll from the IronPython/IronRuby.
  • Q3 : Is there any easy way to use IronPython/IronRuby functions from C# or F#?

Any example code would be great.


回答1:


1) Yes. Using a simple example, in F#, I can invoke the main method of a C# console app:

open SampleApp

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

2) For the DLR, I think things are a bit different. I believe this is how you would execute Python (for example);

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. They should be available from the Microsoft DLR site.




回答2:


I was told that the assembly files made from C# and F# source is interoperable as they are compiled into .NET assembly.

Yes, you can reference an F# assembly in a C# one. The F# objects etc will function like C# ones when you reference them.

You can access IronPython/Ruby code in C# too, although it's a little different than just referencing an assembly. Here is an example:

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




回答3:


Q3: Is there any easy way to use IronPython/IronRuby functions from C# or F#?

You might have a look at Embedding IronPython into a C# application

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"));


来源:https://stackoverflow.com/questions/2969194/integration-of-c-f-ironpython-and-ironruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!