xInterop .NET Bridge: Calling .NET assembly by using reflection from native C++

Native C++ application calls into C# .NET classes, methods using .NET reflection

With xInterop .NET Bridge technology, it is quite easy to access the C# .NET classes, properties, instance methods and static methods from native C++ application using .NET reflection.

The C++ bridge DLL generated by xInterop .NET bridge is strong-typed, which means all the C++ bridge class is mapped to a specific .NET class. By design, the methods of any class created in the C++ are the public methods of the .NET class. If you simply want to create a native C++ DLL wrapping around an existing .NET assembly at compile-time statically, you have nothing to worry, there is no need to use C# .NET reflection at all, the C++ header file created by xInterop .NET Bridge will have everything defined including all strong-typed classes for you to call into the original C# .NET classes, methods and properties.


What if I want to call the protected method or private method like I would in the .NET managed world using reflection?

What if I want to call a method from another assembly which I have not created a strong-typed C++ DLL for?

What if I want to call a method whose name is only known run-time?

What if I want to call a method from a .NET assembly loaded dynamically?

Thanks to xInterop .NET Bridge and .NET reflection technology, we will be able to achieve all of the above by simply creating a C++ bridge DLL for a few reflection-related classes in System.Reflection namespace.

With xInterop .NET Bridge, you will be able to load any .NET assembly and call into any class and any method from the .NET assembly.  The way we call the .NET methods from C++ will be same as the way calling from .NET except the difference that we will be using the C++ bridge classes of the reflection-related classes, most notable as shown below,

  • Type, type class is used to retrieve whatever class we would like to call via .NET reflection
  • MethodInfo, MethodInfo is used to retrieve the information of specific method which is then called by using Invoke method via .NET reflection.

Both classes shall have matching C++ bridge class created so that we will be able to call them from native C++ using .NET reflection through the C++ bridge DLL and .NET bridge assembly.

For a simple demonstration, this is all we need.

The following C# code is the little application if we would call method using .NET reflection.

Immediately after the C# code, I would like to show you how neat the corresponding C++ code is to call the same method using C++ bridge classes.

Let’s start xInterop .NET Bridge and creates the native C++ Bridge DLL step by step.

Step 1: Create a C# library project which contains the reference to the required .NET classes

xInterop .NET Bridge currently requires a .NET library assembly as input to generate a corresponding C++ native bridge DLL, we think it is the natural way and the best way to create a C/C++ bridge native DLL.


Step 2: Create xInterop .NET Bridge project and build C++ bridge DLL and .NET bridge assembly.

After running xInterop .NET Bridge, we will create a native C++ bridge DLL and a .NET bridge assembly for the original TypeDemo .NET assembly, and also a testing application so that we can instantly verify if everything works fine.

What we are interested in seeing in the C++ header file is NType class and MethodInfo class.


xInterop .NET Bridge shall create both MethodInfo and MethodBase since we set “Depth of inheritance” to 1, which means including the direct base class of any class whose reference is  found in the original C# .NET assembly.

Step 3: Running the testing application.

Opening the newly created testing application named “TestApp”, it should look like the following, everything has been taken care of, such as loading the .NET bridge assembly and the original .NET assembly automatically.

This is the final resulting console application looks like and its output.

A very useful C++ debugging tip, if you plan to debug a native C++ application release version, you will need to turn off the optimization of the console application, otherwise, you will not be able to watch or monitor most of the variables because the code has been optimized and its code is not the same as the one you are reading in the Visual Studio IDE C++ code editor.