2011-12-20 27 views
4

我得到異常的對象引用需要非靜態字段,方法,或...無法調用CLR的Clojure從C#應用程序的靜態方法

這裏什麼我試圖做:

;; 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. 

回答

4

在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. 將program.exe和program.clj.dll從步驟1的輸出添加到C#項目中。
  3. 構建C#項目。

這應該有效。我已經用vs clojure和我向你展示的方式建立了兩個。我使用clojureclr 1.3

希望幫助, 羅布

+0

我有兩個引用。據我記得這是應用程序崩潰在第一個例子上啓動program.exe並確保one.exe上的appcrash。 Clojure.compile.exe輸出:D:\ nCdy \ clojure-clr-intro \ 3-calling-clojure-from -c-sharp \ clj \ one> Clojure.Compile.exe一個 編譯一個。 - 797毫秒。但是當試圖從C#運行時,無法運行one.exe(因爲appcrash)和絕對相同的「非靜態字段所需的」。我會盡力發現更多信息。 – Cynede 2011-12-20 12:54:15

+0

當你運行它時,你能告訴我你得到的錯誤嗎?你能沒有錯誤地運行clojure.main.exe? – Rob 2011-12-22 19:58:29

+0

我可以從visual studio運行它,但是當我直接運行program.exe時,我在kernelbase.dll上得到了appcrash:System.TypeInitializationException未處理。消息:鍵入初始化程序「程序」引發異常。我還添加了加載日誌到問題。 – Cynede 2011-12-23 04:05:12

相關問題