IKVM.NET Wiki
Brought to you by:
jfrijters
{{DeveloperGuide}}
Java does not support the concept of properties like C#. If you write a .NET library than it can be nice to have properties in your public API.
At compile time you can use a map.xml to modify the compiled result. The follow sample demonstrate the use of map.xml to define a property.
public class IntList{
private int[] list;
public IntList(int size){
list = new int[size];
}
public void setItem(int index, int value){
list[index] = value;
}
public int getItem(int index){
return list[index];
}
}
<root>
<assembly>
<class name="IntList">
<attribute type="System.Reflection.DefaultMemberAttribute, mscorlib" sig="(Ljava.lang.String;)V">
<parameter>Item</parameter>
</attribute>
<property name="Item" sig="(I)I">
<getter name="getItem" sig="(I)I" />
<setter name="setItem" sig="(II)V" />
</property>
</class>
</assembly>
</root>
When IntList.java is compiled to IntList.class and then ikvmc'ed using:
ikvmc IntList.class -remap:map.xml
The resulting IntList.dll will be usable from C# like this:
IntList l = new IntList(10);
l[4] = 42;
Console.WriteLine(l[4]);
Valdemar Mejstad created a tool to automatically generate the xml to define properties based on Java's java.beans.BeanInfo. The source is available here: MapFileGenerator.java.
If you want create a property for a class member then you can use the annotation.
@ikvm.lang.Property(get = "get_Handle")
private long Handle;