Cannot call Clojure static clr method from C # application

I get an exception An object reference is required for a non-static field, method or ...

here is what i am trying to do:

;; Here 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 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 it turned out: Error 1 An object reference is required for a non-static field, method or property 'program.hi ()' ConsoleApplication1.

Same thing here: https://github.com/nCdy/clojure-clr-intro/blob/master/3-calling-clojure-from-c-sharp/csharp/clj_in_csharp/Program.cs

I am using the Visual Studio vsClojure built-in extension . 1.1.0; The latest version is version 1.1.0 after the Readme GitHub file. Maybe I need to update it, but I have some local problems that prevent me from building the last clojre-clr (I will solve them next year).

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

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. 
+4
source share
1 answer

In a C # project, do you have a link to program.exe and program.clj.dll?
If you build the clojure project yourself and run it, will this work for you?

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

  • Run the clojure.compile program in the directory where program.clj lives.
  • Add the program.exe and program.clj.dll file from the output of step 1 to your C # project.
  • Create a C # project.

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

Hope this helps, Rob

+4
source

Source: https://habr.com/ru/post/1387146/


All Articles