# Saturday, March 20, 2004
« A Less Broken Snapshot? | Main | Q & A »

Miguel posted a nice example of how to use Gtk# from Java using IKVM/Mono on his blog. In response Pablo posted a question to the Mono list and Jonathan Pryor replied with a nice explanation of how delegates are handled to the IKVM and Mono lists (quoted with permission, slightly edited):

From: Jonathan Pryor
Sent: Friday, March 19, 2004 02:35
To: Pablo Baena
Cc: Miguel de Icaza; mono-list@lists.ximian.com; ikvm-developers@lists.sourceforge.net
Subject: Re: [Mono-list] Java and C#


On Thu, 2004-03-18 at 16:07, Pablo Baena wrote:
> Miguel: I saw your blog about IKVM. One thing I haven't been able to 
> investigate is, how useful can be Gtk# with Java. Because, for example, I 
> couldn't find a clue on how to attach a Java 'listener' to a C# event, or any 
> way to use attributes in Java.

They really need to document this better...

However, grepping through the ikvm.zip file (from their website), we

// file: classpath/java/lang/VMRuntime.java
cli.System.AppDomain.get_CurrentDomain().add_ProcessExit (
  new cli.System.EventHandler (
    new cli.System.EventHandler.Method () {
      public void Invoke (Object sender, cli.System.EventArgs e) {

>From this (and prior knowledge), we can draw the following statements:

1. Properties are actually functions with `get_' and `set_' prefixed to
them. Thus C# property System.AppDomain.CurrentDomain is the static
Java function cli.System.AppDomain.get_CurrentDomain().

2. Events are actually functions with `add_' and `remove_' prefixed to
their name. Thus C# event System.AppDomain.ProcessExit is the static
Java function cli.System.AppDomain.add_ProcessExit().

3. There is no equivalent to C# delegates in Java, so these are
translated into a class + interface pair. The EventHandler class is the
standard C# type name (cli.System.EventHandler), which takes as an
argument an interface to invoke, named "cli." + C# delegate type name +
".Method", hence cli.System.EventHandler.Method. The EventHandler.Method
interface has a function Invoke() which must be implemented, and this
method will be invoked when the event is signaled.

I suspect that there is no way to add attributes in Java. Microsoft's
Visual J# permits the use of Attributes (IIRC), but it's through their
Visual J++ syntax -- through a specially formed JavaDoc comment. 
Something like (from memory):

* @attribute-name (args...)
public void myMethod () {/* ... */}

Of course, that's compiler specific, and no standard Java compiler will
support that. So when it comes to attributes, you're probably up the

- Jon

I replied saying that I believe that the attribute construct in JDK 1.5 can probably be used to expose .NET attributes to Java (and use them in Java code that is target to run on IKVM).

Saturday, March 20, 2004 2:51:08 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [3] Tracked by:
"Free Strip Poker Online Game" (Free Strip Poker Online Game) [Trackback]

Wednesday, March 24, 2004 7:03:17 PM (W. Europe Standard Time, UTC+01:00)
Suppose I'm trying to write Java code that's designed to work with IKVM.

Given an instance of System.Type, how do I determine what the typename is in Java?

With the old model, I think, it went something like this:

System.Object => java.lang.Object
System.Exception => java.lang.Throwable
System.String => java.lang.String
Any type defined in java code, or with the attribute that turns off name mangling => don't change the name
Any other type => prepend "cli."

With the new model, there are more cases, and I don't know the answers to some of them.

java.lang.Object => java.lang.Object
java.lang.Throwable => java.lang.Throwable
System.Object => cli.System.Object or java.lang.Object depending on what I'm doing with it
System.Exception => cli.System.Exception or java.lang.Throwable depending on what I'm doing with it
Other types as before

Obviously, the "depending on what I'm doing with it" is the tricky part. Do you have, off the top of your head, a list of when it's appropriate to use cli.System.{Object,Exception} in Java code and when it's appropriate to use java.lang.{Object,Throwable} instead? I'm guessing most of the time the answer is java.lang.*, but I fear that the definition of which times you *don't* want to do that is a hard one to specify in precise terms.

What would be really nice would be if there were something in the IK.VM.NET.dll that would let me answer this question authoritatively with a simple call...
Wednesday, March 24, 2004 7:29:53 PM (W. Europe Standard Time, UTC+01:00)
Oh, one other thing that occurred to me would be a nice feature: if a class implemented in Java could put itself into the cli.* package and thereby make itself look to other Java code as if it were a .NET type, without actually putting it in a cli.* namespace in .NET. In other words, a Java class cli.Foo.Bar would be compiled as namespace Foo.Bar *without* the attribute that preserves its name in Java, so that Foo.Bar then gets translated back to cli.Foo.Bar when Java code sees it.

Sort of the inverse of the attribute for turning name mangling off on the .NET side.
Thursday, March 25, 2004 6:46:11 AM (W. Europe Standard Time, UTC+01:00)
Are there any tasks for a CS scrub like myself to work on with IKVM?
Comments are closed.