从.NET调用的ClojureNET、Clojure

由网友(♀乖乖瀦♀)分享简介:我已经花了一些时间使用Clojure-CLR播放。我REPL是工作,我可以从Clojure的调用.NET类,但我一直没能找出调用从C#类编译Clojure的动态链接库。 我一直在努力适应Java示例发现这里: 我删除了:从这个例子的顶部名称行,因为它会导致重复键:名称的错误。如果没有:名称行中,code编译好,我可以添...

我已经花了一些时间使用Clojure-CLR播放。我REPL是工作,我可以从Clojure的调用.NET类,但我一直没能找出调用从C#类编译Clojure的动态链接库。

我一直在努力适应Java示例发现这里:

我删除了:从这个例子的顶部名称行,因为它会导致重复键:名称的错误。如果没有:名称行中,code编译好,我可以添加在Visual Studio中的参考,但我似乎无法弄清楚如何使用code。我已经尝试了各种使用声明的,但至今没有奏效。任何人都可以提供这一点见解?这里是Clojure的code,我尝试使用。

 (NS code.clojure.example.hello
  (:创一流
   :方法[#^ {:静态真实}输出[INT INT] INT]))

(defn输出[A,B]
  (+ A,B))

(defn  - 输出
  [A,B]
  (输出B))
 

解决方案

我能得到它的工作做了以下内容:

首先,我改变了你的codeA一点,我在和命名空间的麻烦和编译器思考点是目录。所以,我结束了这一点。

 (NS打招呼
  (:要求[clojure.core])
  (:创一流
   :方法[#^ {:静态真实}输出[INT INT] INT]))

(defn输出[A,B]
  (+ A,B))

(defn  - 输出[A,B]
  (输出B))

(defn -main []
  (调用println(STR(+ 10 10):(输出5 10))))
 

接下来,我通过调用编译它:

Clojure.Compile.exe你好

Clojure学习笔记 3 使用lein创建Clojure项目的过程

这将创建几个文件:hello.clj.dll,hello.clj.pdb,用hello.exe和hello.pdb您可以执行hello.exe的,它应该运行-main功能

接下来,我创建了一个简单的C#控制台应用程序。然后添加以下引用:Clojure.dll,hello.clj.dll,并用hello.exe

下面是控制台应用程序的code:

 使用系统;

命名空间ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            你好H =新的Hello();
            的System.Console.WriteLine(h.output(5,9));
            System.Console.ReadLine();
        }
    }
}
 

正如你所看到的,你应该能够创建和使用的Hello类,它驻留在hello.exe的组装。我不为什么功能输出也不是一成不变的,我认为它在CLR编译器的错误。我也不得不使用1.2.0版本ClojureCLR作为最新被扔总成没有发现异常。

为了执行应用程序时,请务必设置clojure.load.path环境变量到你的Clojure二进制文件驻留。

希望这有助于。

I have been spending some time playing with Clojure-CLR. My REPL is working, I can call .NET classes from Clojure, but I have not been able to figure out calling compiled Clojure dlls from C# classes.

I have been trying to adapt the java example found here:

I removed the :name line from the top of the example because it causes a "Duplicate key: :name" error. Without the ":name" line, the code compiles fine and I can add the reference in Visual Studio, but I can't seem to figure out how to use the code. I've tried a variety of 'using' statements, but so far nothing has worked. Can anyone provide a little insight on this? Here is the Clojure code I am attempting to use.

(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))

解决方案

I was able to get it to work doing the following:

First I changed your code a bit, I was having trouble with the namespace and the compiler thinking the dots were directories. So I ended up with this.

(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))))

Next I compiled it by calling:

Clojure.Compile.exe hello

This creates several files: hello.clj.dll, hello.clj.pdb, hello.exe, and hello.pdb You can execute hello.exe and it should run the -main function.

Next I created a simple C# console application. I then added the following references: Clojure.dll, hello.clj.dll, and hello.exe

Here is the code of the console app:

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();
        }
    }
}

As you can see, you should be able to create and use the hello class, it resides in the hello.exe assembly. I am not why the function "output" is not static, I assume it's a bug in the CLR compiler. I also had to use the 1.2.0 version of ClojureCLR as the latest was throwing assembly not found exceptions.

In order to execute the application, make sure to set the clojure.load.path environment variable to where your Clojure binaries reside.

Hope this helps.

阅读全文

相关推荐

最新文章