[Mono-list] Java and C#

Jonathan Pryor jonpryor@vt.edu
Thu, 18 Mar 2004 20:35:09 -0500


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.  (This could actually
be a class, but Java convention is for these to be interfaces, so I'm
assuming they're interfaces.  I'd have to compile a sample app and
disassemble it to be sure.)  The EventHandler class/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