您的位置:首页 > 编程语言 > Java开发

IKVM.NET_第十一篇_开发者指南_Java中开发.NET应用程序

2010-07-04 16:14 656 查看
本文讨论在Java中开发.NET应用程序。内容如下:
1) 概述
2) 把.NET API映射(Map)到Java
3) 属性
4) 迭代
5) 托管和事件处理
6) 可变长度参数
+++ 概述
IKVM使得用Java语言开发.NET应用程序。下面介绍它是如何工作的:
第一步:在应用程序中识别.NET类;
第二步:之后,识别.NET dll中包含的第一步识别的.NET类;
第三步:使用ikvmstub应用程序来为第二步中每个DLL文件生成Java jar文件;
ikvmstub工具分析DLL文件中的.NET类,并生成包含Java接口和存根类的jar文件。Java源代码编译器需要这些信息,但是对于.NET装配文件一无所知。
第四步:使用javac或jikes,并在编译器classpath中指定ikvmstub产生的jar文件,编译Java代码;
第五步:使用ikvmc编译Java字节码。使用“-reference”选项指定你引用的dll文件,但不包含ikvmstub生成的jar文件。
+++把.NET API映射(Map)到Java
++ 当ikvmstub生成一个存根jar文件时,它必须防止Java API类与存根类之间命名空间的冲突,这就必须将.NET的特点,如属性、托管、迭代和可变长度的参数列表等映射到Java语言。
++ 为了防止命名空间冲突,ikvmstub通过在.NET命名空间加上“cil.”前缀来创建Java包名。例如,在System.IO命名空间中的一个.NET类,被ikvmstub转换为Java jar包的名字是“cli.System.IO”。因此,当你使用“System.IO.File”类写Java代码时,你应该在你的Java代码中使用下面的import声明:
import cli.System.IO.*;
import cli.System.IO.File;
++ 下面的小节将讨论.NET的特性如何映射到Java语言。有一些映射相当直接,比如属性和迭代。而其他的一些映射就需要做一点工作,比如托管和事件处理。
+++ 属性
因为,Java没有直接的语言来支持属性,因此,ikvmstub映射.NET属性到Java时,需要使用get或set方法。比如,一个用C#定义的属性:
public datatype property-name
{
get { ... }
set { ... }
}
解释成Java stub方法,如下:
public datatype get_property-name( ) { ... }
public void set_property-name(datatype value) { ... }
下面用C#写的属性的代码,如何在Java中访问:
C# 代码:
int weight = bear.Weight;
bear.Weight = 15;
Java 代码:
int weight = bear.get_Weight();
bear.set_Weight(15);
+++ 迭代
略。www.ikvm.net
+++ 托管和事件处理
略。www.ikvm.net
+++ 可变长度参数
略。www.ikvm.net

Developing .NET Applications in Java
This section discusses information you need to know when you want to develop .NET applications in Java.
1) Overview
2) Mapping .NET API's to Java
3) Properties
4) Enumerations
5) Delegates and Event Processing
6) Varargs
+++ Overview
IKVM makes it possible to develop .NET applications using the Java language. Here's how it works:
1) Identify .NET classes you want to use in your application.
2) Identify which .NET dll's contain the .NET classes you identified in step 1.
Tip: If you're developing on Windows, the Microsoft .NET SDK Class Reference documentation identifies the assembly / dll for a .NET class at the bottom of each class overview page.
3) Use the ikvmstub application to generate a Java jar file for each dll you identified in step 2.
The ikvmstub tool analyzes the .NET classes in the designated dll and generates a jar file containing Java interfaces and stub classes. This information is needed by the Java source compiler, which knows nothing about .NET assemblies.
4) Compile your Java source code using javac or jikes, with the ikvmstub-generated jar files on the compiler classpath.
5) Compile the resulting Java classes using ikvmc. Use the -reference option to reference the dll's containing the .NET classes you used; do not include the ikvmstub-generated jar files on the compiler classpath.
+++ Mapping .NET API's to Java
++ When ikvmstub generates a stub jarfile, it has to prevent namespace conflicts between Java API classes and generated stub classes. It must also map .NET features such as properties, delegates, enumerations, and variable-length argument lists to Java language equivalents.
++ To prevent namespace conflicts, ikvmstub creates Java package names from .NET namespaces by prefixing them with cli. For example, a .NET class in the System.IO namespace would have a stub generated for it in a Java package named cli.System.IO. So, when writing Java code that uses the System.IO.File class, you would use one of the following import statements in your Java code:
import cli.System.IO.*;
import cli.System.IO.File;
++ The following sections discuss how .NET features are mapped to the Java language. Some of the mappings, such as properties and enumerations, are fairly straightforward. Others, such as delegates and event handling, require a little more work.
++ Tip: Java development tools that provide code assist features are a great help when writing applications that use .NET API's. If you install the ikvmstub-generated jar files into your favorite Java IDE, you can use code completion to help you use the .NET methods, properties, and enumerations correctly. Note, however, that you will not be able to test your applications using your Java IDE debugger.
+++ Properties
++ Since Java has no direct language support for properties, ikvmstub maps .NET properties to Java getter and setter methods. A .NET property defined in C# like this:
public datatype property-name
{
get { ... }
set { ... }
}
++ would be translated to a pair of Java stub methods, like this:
public datatype get_property-name( ) { ... }
public void set_property-name(datatype value) { ... }
++ Here is an example of C# code that uses a property, and how you would access the same property in Java:
C# code:
int weight = bear.Weight;
bear.Weight = 15;
Java code:
int weight = bear.get_Weight();
bear.set_Weight(15)
+++ Enumerations
TODO. For now, see the IKVM Weblog, March 20, 2004 entry.
+++ Delegates and Event Processing
TODO. For now, see the IKVM Weblog, March 20, 2004 entry. Also, see the winforms sample.
+++ Varargs
TODO. For now, see the IKVM Weblog, March 20, 2004 entry. Also, see the usenetapi/CreateFile.java sample.by Stephen Schaub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐