我遇到异常非静态字段、方法或……需要对象引用
这是我想做的:
;; 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 需要对象引用。
我正在使用内置 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 获取最新的二进制版本.完成设置后,请执行以下操作:
- 在program.clj所在目录运行clojure.compile程序。
- 将步骤 1 的输出中的 program.exe 和 program.clj.dll 添加到您的 C# 项目。
- 构建 C# 项目。
那应该行得通。我已经用 vs clojure 和上面展示的方式构建了两者。我正在使用 clojureclr 1.3
希望对你有帮助, 罗布
关于c# - 无法从 C# 应用程序调用 Clojure clr 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8571242/