Search code examples
c#.netclojureclojureclr

Can't call Clojure clr static method from C# application


I'm getting exception An object reference is required for the non-static field, method, or …

here is what am I trying to do :

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

But got: Error 1 An object reference is required for the non-static field, method, or property 'program.hi()' ConsoleApplication1.

The same I have here: https://github.com/nCdy/clojure-clr-intro/blob/master/3-calling-clojure-from-c-sharp/csharp/clj_in_csharp/Program.cs

I'm using build-in Visual Studio extension vsClojure. 1.1.0; Latest release version 1.1.0 following GitHub readme file. Maybe I need to update it but I've got some little local problems which gives me not to build last clojre-clr (I will solve them next year).

So the question is how can I call clojure clr library from C# and where is my trouble?

log :

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

Solution

  • In the c# project do you have a reference to program.exe and program.clj.dll?
    If you build the clojure project by itself and run it does it work for you?

    Another thing you can try is get the latest binary build At https://github.com/downloads/richhickey/clojure-clr/clojure-clr-1.3.0-Debug-4.0.zip . Once you get that setup do this:

    1. Run clojure.compile program in the directory where program.clj lives.
    2. Add the program.exe and program.clj.dll from the output of step 1 to your C# project.
    3. Build the C# project.

    That should work. I've built both with vs clojure and the way I showed you above. I am using clojureclr 1.3

    Hope that helps, Rob