Passing reference-type arguments
With Javonet, you can very easily pass any object from .NET Framework DLL as argument to any method from .NET Framework DLL, or substitute a field or property from .NET Framework DLL. Instances of objects from .NET Framework DLL can be stored in your Java application using the special NObject\JObject type variable. When passing the NObject\JObject object to the called method, Javonet just pass it as a reference.
Example:
I code in:
// Todo: activate Javonet
// “Now” field gets a value from DateTime
NObject nowDateObj = Javonet.getType("DateTime").get("Now");
// create a new DateTime value, passing a date of 1980-01-01 as constructor arguments
NObject date = Javonet.New("DateTime", 1980, 1, 1);
// subtract two dates
// The datesDiff variable will store an instance of the TimeSpan object,
// that holds the difference between now and 1980-01-01
NObject datesDiff = nowDateObj.invoke("Subtract", date);
// write response to console
String result = datesDiff.invoke("ToString");
System.out.println(result);
Was this article helpful?