How to use IKVM to integrate Java to DotNet

Using Ja.Net to integrate Java and DotNet(c#): Basic

Using Ja.Net to integrate Java and DotNet(c#): Class and Type

Using Ja.Net to integrate Java and DotNet(c#): More detailed basic issues

Using Ja.Net to integrate Java and DotNet(c#): Encoding and Properties

Using Ja.Net to integrate Java and DotNet(c#): Class implementing and inheriting

Using Ja.Net to integrate Java and DotNet(c#): Assemblies in Java Source code

Using Ja.Net to integrate Java and DotNet(c#): IO in Java and DotNet

Encoding

That’s great that I wrote a simple case which output some Chinese characters to Console window, the source code was:

    public static void main(String[] argvs)
        throws Exception
    {
        System.out.println("<a>adas中文d<AA vv='值'/><b id='asd'/></a>");
    }

Let’s compile it and run it, what should we get?

javac -d ja.net -nowarn java
java -classpath ja.net testcase
<a>adas?D??d<AA vv='?μ'/><b id='asd'/></a>

All Chinese characters were encoded into mess.

After checked the help documentation of Javac, we must add a encoding parameter to tell the compiler that there are some file encoded with the specified char-set.

The new command line to compile after fixed is:

javac -encoding GB2312 -d ja.net -nowarn java
java -classpath ja.net testcase
<a>adas中文d<AA vv='值'/><b id='asd'/></a>

Properties from DotNet

From DotNet framework V2, C# provides a pragmatic feature called “properties” to simply Beans’ access, (The term “Bean” is from Java). But unfortunately, no one of Java versions support this feature. Let’s go back to checking what have been done in the byte-code when we add some “properties” into classes:

NF30003 

The preceding snapshot is from the class “System.IO.FileInfo”, as you can see, the DotNet compiler generated two backend methods with prefix named “get_” and “set_”. In Java source code, if you want to use classes which contain some properties and generated by DotNet, you have to invoke the functions with prefix rather than the name of properties. Here is a simple sample, maybe it makes this issue more clear.

//C# source code

public class A

{

public String Name{get;set;}

public String Id{get;set;}

}

//Java Source code

A a=new A();

String name=a.Name;//Does not work, compiling error, can not found the symbol “Name” from class “A”

String name=a.get_Name();//Works

a.set_Name(“New Name”);//Works


Jeason Zhao (沈胜衣,斛律光) ------雪饮再现,一个人的江湖
我知道我是谁,我是沈胜衣,默默的活着,就像空气。