3

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.
cnd
  • 32,616
  • 62
  • 183
  • 313

1 Answers1

3

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

Rob
  • 166
  • 1
  • 4
  • I have references to both. As far as I remember it was app crash on start program.exe on first example and sure appcrash on one.exe. Clojure.compile.exe output : D:\nCdy\clojure-clr-intro\3-calling-clojure-from-c-sharp\clj\one>Clojure.Compile.exe one Compiling one to . -- 797 milliseconds. But can't run one.exe (because appcrash) and absolutely same " required for the non-static field" when trying to run from C#. I will try to discover more info. – cnd Dec 20 '11 at 12:54
  • can you show me the errors you get when you run it? Can you run clojure.main.exe without error? – Rob Dec 22 '11 at 19:58
  • I can run it from visual studio but when I run program.exe directly I'm getting appcrash on kernelbase.dll : System.TypeInitializationException was unhandled. Message: Type Initializer "program" threw an exception. I also added loading log to the question. – cnd Dec 23 '11 at 04:05
  • Outside of visual studio what happens if you run clojure.main.exe from a command prompt? Does it start up without error? If so what clojure version does it say you are running? In the C# project what references do you have? Compare them against the ones I've listed in my latest blog post. http://www.myclojureadventure.com/2011/12/intro-to-clojure-clr-calling-clojure.html – Rob Dec 26 '11 at 19:12