Casting

This article provides an introduction to cross-technology handling of casting mechanism. Casting is necessary in statically-typed languages like C, C++, Java, and C#, where the data type of a variable is known at compile time. In these languages, if you want to assign a value of one type to a variable of another type, you need to perform a cast.

For example, if you have an integer and you want to use it as a float, you would need to cast it to a float. This is done because different data types may be represented differently in memory, and casting ensures that the data is interpreted correctly.

In contrast, dynamically-typed languages like Python or JavaScript perform most of the casting automatically, and explicit casting is less common.

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 objects and methods from .NET DLL like they were available in Perl but invocation must be performed through Javonet SDK API.

Custom .NET DLL showing casting

With Javonet it is possible to reference any custom .NET DLL and interact with its methods declared on types defined within that module almost the same as with any other Perl library.

Snippet below represents the sample code from .NET DLL which contains a few methods with the same name but different parameter type. Each of these methods can perform different logic so it is necessary to invoke intended method.

public static string CastSampleMethod(System.UInt32 value)
{
	return "CastSampleMethod with System.UInt32 called";
}

public static string CastSampleMethod(System.Single value)
{
	return "CastSampleMethod with System.Single called";
}

public static string CastSampleMethod(System.Double[] value)
{
	return "CastSampleMethod with System.Double[] called";
}

It is possible to invoke each of these methods by casting the value from perl to proper type specified in .NET DLL.

# use activate only once in your app
Javonet->activate("your-license-key");

# create Netcore runtime context
my $called_runtime = Javonet->in_memory()->netcore();

# set up variables
my $library_path = "${resources_directory}/TestClass.dll";
my $class_name = "TestClass.TestClass";

# load Netcore custom library
$called_runtime->load_library($library_path);

# get type from the runtime
my $netcore_type = $called_runtime->get_type($class_name)->execute();

# get type for casting
my $target_type = $called_runtime->get_type("System.UInt32")->execute();

# invoke static method
my $response = $netcore_type->
    invoke_generic_static_method("CastSampleMethod",
        $called_runtime->cast($target_type, 5.2))->
    execute();

# get value from response
my $result = $response->get_value();

# print result to console
print("$result\n");

In the snippet above, user intended to invoke CastSampleMethod with argument of specific type. Value from perl has been casted to proper type and passed to CastSampleMethod. Second example shows the same steps but for different parameter type.

# use activate only once in your app
Javonet->activate("your-license-key");

# create Netcore runtime context
my $called_runtime = Javonet->in_memory()->netcore();

# set up variables
my $library_path = "${resources_directory}/TestClass.dll";
my $class_name = "TestClass.TestClass";

# load Netcore custom library
$called_runtime->load_library($library_path);

# get type from the runtime
my $netcore_type = $called_runtime->get_type($class_name)->execute();

# get type for casting
my $target_type = $called_runtime->get_type("System.Float")->execute();

# invoke static method
my $response = $netcore_type->
    invoke_generic_static_method("CastSampleMethod",
        $called_runtime->cast($target_type, 5))->
    execute();

# get value from response
my $result = $response->get_value();

# print result to console
print("$result\n");

In the snippet above, user intended to invoke CastSampleMethod with argument of specific type. Value from perl has been casted to proper type and passed to CastSampleMethod.

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.