简体   繁体   English

从Java运行C#代码,反之亦然

[英]Run C# code from Java and vice-versa

I have a game server which is made in Java. 我有一个用Java制造的游戏服务器。 I want to make a plugin system that loads a .NET DLL and calls events / functions inside that DLL, then inside those I'll have to call functions in the game server (Java). 我想制作一个加载.NET DLL并在该DLL中调用事件/函数的插件系统,然后在其中必须调用游戏服务器(Java)中的函数。 The only part that is giving me trouble at the moment is how to interface java and a .NET dll. 目前唯一给我带来麻烦的部分是如何接口Java和.NET dll。

I've been searching and found some things but they were all based on products and I want to make my own interface for that. 我一直在搜索并找到了一些东西,但是它们都是基于产品的,我想为此创建自己的界面。 Ah, not to mention it needs to have high performance, the code will be called a lot of times in a second if it has to. 嗯,更不用说它需要高性能,如果有必要,该代码将在一秒钟内被调用很多次。 Could someone point or give-me ideas how could I work this out? 有人可以指出或给予我一些想法,我该如何解决?

EDIT: 编辑:

To make it more explicit: 为了使其更明确:

  1. Game Server (Java application) calls a function in .NET dll 游戏服务器(Java应用程序)在.NET dll中调用一个函数
  2. The .NET function just called by java, calls multiple functions from Game Server (Java Application). Java刚刚调用的.NET函数从Game Server(Java应用程序)调用多个函数。

Take a look at jni4net if you're targeting Windows. 如果您以Windows为目标,请看看jni4net It's an alpha quality release, but Robocode already uses it to run .NET robots inside the Java runtime. 这是一个Alpha质量版本,但是Robocode已经使用它在Java运行时内部运行.NET机器人。

Another option is to use a high-performance messaging approach. 另一种选择是使用高性能的消息传递方法。 You'll need a second process - likely a .NET plug-in host. 您将需要第二个过程-可能是.NET插件主机。 That process then exchanges messages with the main Java game process. 然后,该过程与主要的Java游戏过程交换消息。 Messaging libraries like 0MQ are pretty darn fast but may not be fast enough for what you have in mind. 0MQ这样的消息传递库非常快,但对于您所想的来说可能不够快。 In addition, you'll have to create a lot of message plumbing which may be cost/time prohibitive. 此外,您还必须创建大量消息管道,这可能会浪费成本/时间。

Try using iKVM: 尝试使用iKVM:

IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. IKVM.NET是Java for Mono和Microsoft .NET Framework的实现。 It includes the following components: 它包括以下组件:

  • A Java Virtual Machine implemented in .NET 在.NET中实现的Java虚拟机
  • A .NET implementation of the Java class libraries Tools that enable Java and .NET interoperability Java类库的.NET实现启用Java和.NET互操作性的工具

http://www.ikvm.net/ http://www.ikvm.net/

If you only have a few methods you are calling you might just use JNI and do it yourself instead of a 3rd Party tool (though I admit I don't know the details of jni4net). 如果您只有几种方法要调用,则可以只使用JNI并自己进行操作,而不要使用3rd Party工具(尽管我承认我不知道jni4net的详细信息)。 Just a word of caution, the project I'm on had to do a similar thing (C# -> C/C++ -> Java via JNI) and we had nothing but problems with it. 提醒一下,我所从事的项目必须做类似的事情(C#-> C / C ++->通过JNI的Java),除了问题,我们什么都没有。 Problems mainly because the java api didn't have any good documentation so that might have been part of it. 问题主要是因为Java api没有任何好的文档,所以它可能是其中的一部分。 If at all possible try to keep it to one language but if that is not possible, make sure you do lots of error checking. 如果有可能,请尝试将其保持为一种语言,但如果无法实现,请确保进行大量错误检查。 When the app crashes, it is very hard to find the problem (unless you own both the java and C# sutff). 当应用程序崩溃时,很难发现问题(除非您同时拥有Java和C#sutff)。 Just my $0.02... 只是我的$ 0.02 ...

Avoid binary coupling. 避免二进制耦合。 Write your own interop layer based on the architectural approach in this article. 根据本文的体系结构方法编写您自己的互操作层。 http://www.codeproject.com/KB/architecture/SerInterfaceXXsd2Code.aspx . http://www.codeproject.com/KB/architecture/SerInterfaceXXsd2Code.aspx For high performance you can replace also the XML serialization mentioned in this article with hessian. 为了获得高性能,您还可以将本文中提到的XML序列化替换为hessian。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM