Create generic Python class instance from .NET DLL

This article provides an introduction to cross-technology handling of generic class instance. A generic class in programming is a class that can work with any data type. The data type is specified as a parameter at the time of creating an instance of the class. This allows for type-safe code reusability. A single class or method can be used with different data types without the need for multiple implementations. It is described in detail in article about generic classes in .NET and article about generics in Java.

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. If have not yet created your first project check Javonet overview and quick start guides for your technology.

With Javonet you can interact with generic classes from .NET DLL like they were available in Python but invocation must be performed through Javonet SDK API.

Javonet allows you to pass any Python value type as argument to method from .NET DLL. For reference type arguments (instances of other classes) you can create such instance with Javonet and pass the Invocation Context variable referencing that object as argument of method invocation.

Generic Python class from framework

It is possible to create generic class instance and interact with its methods from net-dll framework using following Python code:

I code in:
# use activate only once in your app
Javonet.activate("your-license-key")

# create called runtime context
called_runtime = Javonet.in_memory().netcore()

# get type from the runtime
string_type = called_runtime.get_type("System.String").execute()

# get type for generic class
list_type = called_runtime.get_type("System.Collections.Generic.List`1", string_type).execute()

# create instance of generic class
list_instance = list_type.create_instance().execute()

# invoke instance's method
list_instance.invoke_instance_method("Add", "one").execute()
list_instance.invoke_instance_method("Add", "two").execute()
list_instance.invoke_instance_method("Add", "three").execute()
list_instance.invoke_instance_method("AddRange", ["four", "five", "six"]).execute()

# check number of elements in list
response = list_instance.get_instance_field("Count").execute()

# get value from response
result = response.get_value()

# write result to console
print(result)

This snippet uses in memory runtime bridging to start called technology.
Next, instance of generic class is created. While creating instance of .NET generic class it is necessary to pass method name, type and arguments.
While creating instance of Java generic class it is necessary to pass method name and arguments.
Next, instance method are invoked to interact with created instance.
Finally, the result is printed to console.

The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your .NET DLL objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.

Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.