c# - 无法从 C# 应用程序调用 Clojure clr 静态方法

标签 c# .net clojure clojureclr

我遇到异常非静态字段、方法或……需要对象引用

这是我想做的:

;; Here's the program.clj file from a VsClojure project.
;; First thing you need to do is add the :methods property
;; which takes a of methods that are to be generated when
;; the class is created.  Here, I'm generating a static method
;; named hi which has no parameters and returns a String.
;;
;; When program.hi() is called from the C# code the
;; the clojure function -hi will be called.  By default any
;; method in the :methods vector will be mapped to a function
;; with the same name prefixed by a -.  
(ns program
  (:gen-class
   :methods [ #^{:static true} [hi [] String]]))

(defn -hi []
  "Hi from ClojureCLR!")

(defn -main [& args]
  (println "Hello world")
  (println (-hi)))

;; The C# code is nothing special
;; You MUST add both the program.exe and program.clj.dll
;; files as references in hte C# project.  In addition
;; to these assemblies you'll need to add the following
;; assemblies which can be found in the ClojureCLR's directory:
;; clj.gen_class.clj.dll, clojure.clr.io.clj.dll, clojure.core.clj.dll
;; clojure.core_deftype.clj.dll, clojure.core_printl.clj.dll,
;; clojure.core_proxy.clj.dll, clojure.genclass.clj.dll, clojure.gvec.clj.dll


;; using System;

;; namespace csharp
;; {
;;    class Program
;;    {
;;        static void Main(string[] args)
;;        {
;;    // You can see here you'd never know this was
;;    // really a clojure function I'm calling
;;          Console.WriteLine("p.hi => {0}", program.hi());
;;        }
;;    }
;;}

但得到:错误 1 ​​非静态字段、方法或属性“program.hi()”ConsoleApplication1 需要对象引用。

我这里也一样:https://github.com/nCdy/clojure-clr-intro/blob/master/3-calling-clojure-from-c-sharp/csharp/clj_in_csharp/Program.cs

我正在使用内置 Visual Studio 扩展 vsClojure。 1.1.0; GitHub 自述文件之后的最新版本 1.1.0。也许我需要更新它,但我遇到了一些小的本地问题,这让我无法构建最后的 clojre-clr(我将在明年解决它们)。

所以问题是我如何从 C# 调用 clojure clr 库,我的麻烦在哪里?

日志:

'program.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'program.exe' (Managed (v2.0.50727)): Loaded 'D:\nCdy\P\Clojure1\Clojure1\bin\Debug\program.exe', Symbols loaded.
'program.exe' (Managed (v2.0.50727)): Loaded 'D:\nCdy\P\Clojure1\Clojure1\bin\Debug\Clojure.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'program.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'program.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'program.exe' (Managed (v2.0.50727)): Loaded 'D:\nCdy\P\Clojure1\Clojure1\bin\Debug\Microsoft.Scripting.ExtensionAttribute.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'program.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\mscorlib.resources\2.0.0.0_ru_b77a5c561934e089\mscorlib.resources.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

最佳答案

在c#项目中是否有对program.exe和program.clj.dll的引用?
如果您自己构建 clojure 项目并运行它,它对您有用吗?

您可以尝试的另一件事是在 https://github.com/downloads/richhickey/clojure-clr/clojure-clr-1.3.0-Debug-4.0.zip 获取最新的二进制版本.完成设置后,请执行以下操作:

  1. 在program.clj所在目录运行clojure.compile程序。
  2. 将步骤 1 的输出中的 program.exe 和 program.clj.dll 添加到您的 C# 项目。
  3. 构建 C# 项目。

那应该行得通。我已经用 vs clojure 和上面展示的方式构建了两者。我正在使用 clojureclr 1.3

希望对你有帮助, 罗布

关于c# - 无法从 C# 应用程序调用 Clojure clr 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8571242/

相关文章:

c# - 什么约定将 poco 属性自动映射到列?我怎样才能禁用该自动映射?

clojure - 为什么 Clojure 在执行计算后挂起?

c# - 表达式树依赖分析器

c# - 使用并行 FOR 循环节省时间

c# - 具有相同订阅的服务总线多个监听器实例未接收消息

clojure - enlive 中的范围选择器

clojure - 用函数式语言绘制图表

c# - 如何使用 C# 返回序列化为 JSON

.net - 什么是延迟初始化以及它为何有用?

.net - .Net Framework 和 .Net Core 有什么区别?