使用 IKVM 进行回调的 IKVM C# 到 Java 互操作
我开始使用 IKVM 将 Java 库转换为 .NET CIL。我可以成功编写一个 C# 程序,该程序将翻译后的 Java 程序集拉入(inproc)作为引用,并调用翻译后的 Java 代码。
我的问题是,有人熟悉如何使用 IKVM 从 Java 到 C# 进行调用(回调)吗?我一直在寻找好的教程或解释,但还没有看到。
任何帮助表示赞赏。谢谢,
米杰
I've started using IKVM to translate Java libs into .NET CIL. I can successfully write a C# program that pulls in (inproc) a translated Java assembly as a reference and make calls to the translated Java code.
My question is, is anyone familiar w/ how to make calls (callbacks) from Java to C# using IKVM? I've been looking for a good tutorial or explanation but haven't seen one yet.
Any help is appreciated. Thanks,
mj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
女士们先生们,我自己想出了自己的问题。首先是代码,然后是步骤。
Java 类
C# 类
1) 编写 Java 应用程序
2) 将 *.class 文件转换为 jar 文件 (jar -cf myjar.jar *.class)
3) 将 jar 文件转换为 .NET 程序集 (ikvmc -reference:csharp assembly .dll myjar.jar)
此时应该可以工作。您可以运行 C# 程序,让它调用转换后的 Java 程序,反之亦然。注意 ikvmc 调用上的“-reference”标志。这告诉 IKVM 在转换 Java 代码时 csharpassemble.dll 有一些需要注意的类定义。
Ladies and Gentlemen, I figured out my own question. Code first followed by steps.
Java Class
C# Class
1) Write your Java app
2) Convert your *.class files into a jar file (jar -cf myjar.jar *.class)
3) Convert the jar file into a .NET assembly (ikvmc -reference:csharpassembly.dll myjar.jar)
Should work at this point. You can run your C# program, have it call the converted Java program and vice versa. Watch out for the "-reference" flag on the ikvmc call. This tells IKVM when it's converting the Java code that csharpassembly.dll has some class definitions that it needs to watch out for.
JaapM,我认为 mj_ 解决方案中的 CSharpLibrary 是来自第三个 C# DLL 项目 (cshard assembly.dll) 的瞬态类,他首先编译该类,然后在实际的 java 和 C# 代码之间共享。其中包含什么并不重要,其思想是双方都有一段预先已知的代码(类)。如果我是对的话,这太过分了。
我知道,已经过去很长时间了,但我想在这里发布一个适合我的简短解决方案,因为我在上面浪费了太多时间,而且 IKVM 文档非常差:
Java:
不要忘记将 mscorlib.dll 转换为jar 并将其附加到您的 java 项目以支持 cli 导入。
构建它并使用 -target:library 参数在 jar 上运行 ikvmc.exe,并将生成的 DLL 添加到 C# 项目中。
C#:
输出:
JaapM, I think CSharpLibrary in mj_'s solution is a transient class from third C# DLL project (cshardassembly.dll), that he compiles first and then shares between actual java and C# code. It doesn't matter what's in it, the idea is that both sides have a piece of code (class) known in advance. This is overkill, if I'm correct.
I know, it's a long time passed but I would like to post a short solution here that works for me, cuz I wasted too much time on it and IKVM documentation is very poor:
Java:
Don't forget to convert mscorlib.dll into jar and attach it to your java project to support cli imports.
build it and run ikvmc.exe on jar with -target:library parameter and add resulting DLL into C# project.
C#:
output: