Search code examples
javonet

How to create NObject with constructor parameters (JavoNet)


What is the syntax for creating a .Net object from java code (NObject) when the constructor of the .Net object has one or more parameters?

Thanks


Solution

  • The answer by erotavlas is correct although the syntax for classes is much simpler and can be done with a one-liner (https://www.javonet.com/java-devs/guides/creating-instance-calling-instance-methods/).

    To create .NET object from Java you simply use:

    NObject object = Javonet.New("Namespace.ClassName", params...);  
    

    Additional note if your class constructor has array parameter (of any type) you need to cast it to Object array.

    int[] arg1;
    Javonet.New("Namespace.ClassName", new Object[] {arg1})
    

    Also, you can try new service that will create a strongly typed java wrapper for you (have a read here https://www.javonet.com/blog/more-about-javonet-io/)