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# Below... 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 see: // 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) { Runtime.getRuntime().runShutdownHooks(); } } ) ); >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 creek. - 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).
Powered by: newtelligence dasBlog 2.3.12105.0
© Copyright 2021, Jeroen Frijters
E-mail