如何在c#.net中使用java函数? [英] how to use java function in c#.net?

查看:80
本文介绍了如何在c#.net中使用java函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我在C#.net中有一个程序,但我应该在java中编写一个函数。

i想从c#中调用该函数。

我该怎么做?

它可以吗?

请帮帮我。

解决方案

您需要两个级别的间接。您可以使用一些本机代码语言(大多数人使用C ++,但它可以是C,Delphi等等),并使用JNI创建一个本机模块来使用Java代码。请参阅:

http://en.wikipedia.org/wiki/JNI [< a href =http://en.wikipedia.org/wiki/JNItarget =_ blanktitle =New Window> ^ ],

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/index .html [ ^ ]。



当您的本机(非托管)模块完成后,您可以通过P / Invoke在某些.NET程序集中使用它:

http://en.wikipedia.org/wiki/P/Invoke [ ^ ],

http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [ ^ ]。



此CodeProject文章也很有用: http://www.codeproject。 com / csharp / EssentialPInvoke.asp [ ^ ]。



特殊情况是C ++ / CLI等语言。您可以创建一个混合模式项目,托管+非托管。首先,您使用JNI使用Java代码,公开您需要的功能,并将其包装在一些托管的ref类/结构中。它生成的模块可以用作常规.NET程序集:由C#项目引用,依此类推。这样,您可以1)跳过P / Invoke级别,2)使包装类比P / Invoke中的独立静态方法更具表现力。您仍然有两个级别:Java到本机代码,托管在托管中的本机代码。请参阅:

http://en.wikipedia.org/wiki/C %2B%2B / CLI [ ^ ],

http://www.ecma-international .org / publications / standards / Ecma-372.htm [ ^ ],

http:// www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ],

http:// msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ]。



-SA

hi.
I have a program in C#.net but i should write a function in java.
i want to call that function from c#.
how do i do it?
is it posible?
please help me.

解决方案

You need two levels of indirection. You can have some native-code language (most people use C++, but it could be C, Delphi and a lot more), and create a native module using JNI to use Java code. Please see:
http://en.wikipedia.org/wiki/JNI[^],
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/index.html[^].

When your native (unmanaged) module is done, you can use it in some .NET assembly through P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

The special case is such language as C++/CLI. You can create a mixed-mode project, managed + unmanaged. First, you using Java code using JNI, expose functionality you need, and wrap it in some managed "ref" class/structure. It produces the module which can be uses as a regular .NET assembly: referenced by your C# project and so on. This way, you can 1) skip P/Invoke level, 2) make the wrapper class more expressive than the set of independent static methods as in P/Invoke. Still you have two levels: Java to native code, native code wrapped in managed. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA


这篇关于如何在c#.net中使用java函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆