Using C++ enum type from .NET Framework DLL
This article provides an introduction to cross-technology handling of enum
type. In programming, an enum
(short for enumeration) type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Enums are used to create our own data types, just like classes. They are particularly useful when we want to represent a fixed set of constants, such as days of the week, states, colors, directions, and more. This makes the code more readable and less prone to errors.
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 enum
types from .NET Framework DLL as if they were available in C++, but interaction must be performed through the Javonet SDK API. This allows you to handle and manipulate enum
values, pass them to methods, and return them from methods, all while maintaining the type safety and readability of your C++ code.
Custom .NET Framework DLL with C++ enum type
With Javonet it is possible to reference any custom .NET Framework DLL and interact with its methods declared on types defined within that module almost the same as with any other C++ library.
Snippet below represents the sample code from .NET Framework DLL which contains enum
type.
public enum Fruit
{
Apple,
Banana,
Orange,
Mango
}
public static List<Fruit> Fruits = new List<Fruit>();
public static string AddFruitsToList(Fruit[] fruits)
{
Fruits.AddRange(fruits);
return string.Format("{0} fruits on the list", Fruits.Count);
}
Javonet SDK contains various methods to interact with enums and consume the results in C++:
// use Activate only once in your app
Javonet::Activate("your-license-key");
// create called runtime context
auto calledRuntime = Javonet::InMemory()->Clr();
// set up variables
auto libraryPath = resourcesDirectory + "/TestClass.dll";
// load custom library
calledRuntime->LoadLibrary(libraryPath);
// get enum
auto enumType = calledRuntime->GetType("TestClass.TestClass+Fruit");
//get enum's item
auto fruit1 = calledRuntime->GetEnumItem({ enumType, "Mango" });
auto fruit2 = calledRuntime->GetEnumItem({ enumType, "Orange" });
// get item's names and values
auto fruit1Name = fruit1->GetEnumName()->Execute();
auto fruit1Value = fruit1->GetEnumValue()->Execute();
auto fruit2Name = fruit2->GetEnumName()->Execute();
auto fruit2Value = fruit2->GetEnumValue()->Execute();
// get values
auto result1 = std::any_cast<std::string>(fruit1Name->GetValue());
auto result2 = std::any_cast<int>(fruit1Value->GetValue());
auto result3 = std::any_cast<std::string>(fruit2Name->GetValue());
auto result4 = std::any_cast<int>(fruit2Value->GetValue());
// write result to console
std::cout << result1 << ": " << result2 << ", " << result3 << ": " << result4 << std::endl;
This snippet uses in memory runtime bridging to load the .NET Framework DLL and next retrieves reference to specific enum
type. Then two enum
items are created. Their values and names are checked.
Enum items can be passed to .NET Framework DLL method:
// use Activate only once in your app
Javonet::Activate("your-license-key");
// create called runtime context
auto calledRuntime = Javonet::InMemory()->Clr();
// set up variables
auto libraryPath = resourcesDirectory + "/TestClass.dll";
auto className = "TestClass.TestClass";
// load custom library
calledRuntime->LoadLibrary(libraryPath);
//get enum
auto enumType = calledRuntime->GetType("TestClass.TestClass+Fruit")->Execute();
//create enum items
auto apple = calledRuntime->GetEnumItem({ enumType, "Apple" })->Execute();
auto mango = calledRuntime->GetEnumItem({ enumType, "Mango" })->Execute();
// create fruits array
std::vector<std::any> fruitsList = { apple, mango };
// get type from the runtime
auto calledRuntimeType = calledRuntime->GetType(className)->Execute();
// invoke type's static method
auto response = calledRuntimeType->
InvokeStaticMethod("AddFruitsToList", fruitsList)->
Execute();
// get value from response
auto result = std::any_cast<std::string>(response->GetValue());
// write result to console
std::cout << result << std::endl;
In this example, two enum
items are created, too. Then array of enums is created and passed to method which adds enum
items to list.
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 Framework 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.
Was this article helpful?