我已经花了一些时间来玩Clojure-CLR。我的REPL正在运行,我可以从Clojure调用.NET类,但是我无法弄清楚从C#类调用已编译的Clojure dll。
我一直试图适应发现here:的Java示例
我从示例顶部删除了:name行,因为它会导致“Duplicate key::name”错误。没有“:name”行,代码可以很好地编译,我可以在Visual Studio中添加引用,但是我似乎无法弄清楚如何使用代码。我已经尝试了各种“使用”语句,但是到目前为止没有任何效果。谁能对此提供一点见识?这是我尝试使用的Clojure代码。
(ns code.clojure.example.hello
(:gen-class
:methods [#^{:static true} [output [int int] int]]))
(defn output [a b]
(+ a b))
(defn -output
[a b]
(output a b))
最佳答案
我能够做到以下几点:
首先,我稍微更改了代码,我在命名空间和编译器方面遇到麻烦,认为点是目录。所以我结束了这一点。
(ns hello
(:require [clojure.core])
(:gen-class
:methods [#^{:static true} [output [int int] int]]))
(defn output [a b]
(+ a b))
(defn -output [a b]
(output a b))
(defn -main []
(println (str "(+ 5 10): " (output 5 10))))
接下来,我通过调用来编译它:
Clojure.Compile.exe hello
这将创建几个文件:hello.clj.dll,hello.clj.pdb,hello.exe和hello.pdb您可以执行hello.exe,它应该运行-main函数。
接下来,我创建了一个简单的C#控制台应用程序。然后,我添加了以下引用:Clojure.dll,hello.clj.dll和hello.exe
这是控制台应用程序的代码:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
hello h = new hello();
System.Console.WriteLine(h.output(5, 9));
System.Console.ReadLine();
}
}
}
如您所见,您应该能够创建和使用hello类,它位于hello.exe程序集中。我不是为什么函数“输出”不是静态的,我认为这是CLR编译器中的错误。我还不得不使用ClojureCLR的1.2.0版本,因为最新的版本抛出了程序集未发现异常。
为了执行该应用程序,请确保将clojure.load.path环境变量设置为Clojure二进制文件所在的位置。
希望这可以帮助。
关于.net - 从.NET调用Clojure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4380054/