Interoperability between a C++ class and a C# object is a huge issue as is obvious by the fact that C++ has a static object structure and C# has a dynamic object structure. So, lots of interoperable wrapper classes and interfaces have to be used to create communication channels for the two different runtimes. Not so anymore with the introduction of the new DLR (Dynamic Language Runtime).
The DLR of the .Net framework provides types, methods, parameters to be dynamically created with the help of IDynamicMetaObjectProvider, DynamicMetaobject and DynamicObject and another type called ExpandoObject to further enhance dynamism in types by allowing you add members to existing members!
The Expando Object is my chief solution provider for enabling communication between C++ and C# classes.
Given a simple class in C++,
#include
#include
using namespace std;
class person
{
public:
string name;
int age;
};
The solution to enable a C# class to access this C++ is be reading this C++ source file and generating an in-memory XML tree so that this C++ class can be dynamically constructed as a C# class in the DLR and if, for example sake, the C# class wants to assign a name and age to the person class, it can simply write out an XML file that will contain the values. This, then, can be consumed by another C++ class by performing an I/O on the XML file.
I use the Expando object to generate an XML file after reading the C++ source file as follows:
The XML tree
<class name="person">
<Members>
<Member name="name" type="string" scope="public"/>
<Member name="age" type="int" scope="public"/>
</Members>
</class>
What is so special about the usage of the ExpandoObject? The special thing is that the code to generate this XML from a C++ file will work with any number of C++ source files and thus a small piece of code in C# can simplify the complex architecture that is otherwise required for making C++ and C# interoperable.
This XML file is then re-constructed as an in-memory source file and compiled.
Of course, there is a lot more and I have not posted the C# code here as there are many other things that need to be done and the reason I posted this was due to my excitement of having finally found the solution that I had proposed some 5 years back when I was working with Quark Media House and where there was a huge code base in C++.
The DLR of the .Net framework provides types, methods, parameters to be dynamically created with the help of IDynamicMetaObjectProvider, DynamicMetaobject and DynamicObject and another type called ExpandoObject to further enhance dynamism in types by allowing you add members to existing members!
The Expando Object is my chief solution provider for enabling communication between C++ and C# classes.
Given a simple class in C++,
#include
#include
using namespace std;
class person
{
public:
string name;
int age;
};
The solution to enable a C# class to access this C++ is be reading this C++ source file and generating an in-memory XML tree so that this C++ class can be dynamically constructed as a C# class in the DLR and if, for example sake, the C# class wants to assign a name and age to the person class, it can simply write out an XML file that will contain the values. This, then, can be consumed by another C++ class by performing an I/O on the XML file.
I use the Expando object to generate an XML file after reading the C++ source file as follows:
The XML tree
<class name="person">
<Members>
<Member name="name" type="string" scope="public"/>
<Member name="age" type="int" scope="public"/>
</Members>
</class>
What is so special about the usage of the ExpandoObject? The special thing is that the code to generate this XML from a C++ file will work with any number of C++ source files and thus a small piece of code in C# can simplify the complex architecture that is otherwise required for making C++ and C# interoperable.
This XML file is then re-constructed as an in-memory source file and compiled.
Of course, there is a lot more and I have not posted the C# code here as there are many other things that need to be done and the reason I posted this was due to my excitement of having finally found the solution that I had proposed some 5 years back when I was working with Quark Media House and where there was a huge code base in C++.
No comments:
Post a Comment