Creating Instance and Calling Instance Methods

Creating Instance and Calling Instance Methods

JavOnet lets you create instances of any .NET type. To store reference of a particular .NET object, the javOnet-created NObject Java type can be used by variables of any .NET object.

To create an instance of a .NET object, call the Javonet.New method. Once called, you’ve created a reference to that object that could be assigned to NObject variable, which you can then use to perform any operations on that object.

Example

Here’s how to call “Next” method on instance of .NET “Random” class:

	NObject objRandom = Javonet.New("System.Random");
	Integer value = objRandom.invoke("Next",10,20);

	System.out.println(value);

JavOnet calls are very similar to regular .NET or Java calls, with a little bit of reflection style. Value-type results are automatically converted into Java types so you can safely assign them to Java variables. Reference-type results must be assigned to NObject variable.

Any calls to .NET objects using javOnet can be shortened and simplified using our Fluent interface. To find out more, click here.

See Live Example!