Preserving polymorphism across the managed/unmanaged boundary
-
I have a legacy C++ native code application that implements a class hierachy with some inheritance. I have to re-implement the application within a managed framework. TI would like to reuse the unmanged class hierarchy from the managed application and still benefit from the polymorphism built in to my unmanaged class hierarchy. Do I have to write a C++/CLI wrapper for each unmanaged class in the hierarchy and duplicate the inheritance structure within the wrappers?
-
I have a legacy C++ native code application that implements a class hierachy with some inheritance. I have to re-implement the application within a managed framework. TI would like to reuse the unmanged class hierarchy from the managed application and still benefit from the polymorphism built in to my unmanaged class hierarchy. Do I have to write a C++/CLI wrapper for each unmanaged class in the hierarchy and duplicate the inheritance structure within the wrappers?
You might do it that way, but it might actually be better to put the native parts into managed DLLs using the wrappers and then using the DLLs from your managed application. It all depends on what you are trying to do. I wouldn't get too wrapped up in trying to preserve the old hierarchy in the managed world though.
-
I have a legacy C++ native code application that implements a class hierachy with some inheritance. I have to re-implement the application within a managed framework. TI would like to reuse the unmanged class hierarchy from the managed application and still benefit from the polymorphism built in to my unmanaged class hierarchy. Do I have to write a C++/CLI wrapper for each unmanaged class in the hierarchy and duplicate the inheritance structure within the wrappers?
Assuming that you really do need to expose the full class hierarchy as managed types to managed code then yes, I think you're left duplicating the hierarchy with wrapper ref classes. If the client managed application is also guaranteed to be C++/CLI (and not C#) it could #include the native class hierarchy intact and use it directly. If you want to allow C# then I'd use wrapper classes with the PIMPL idiom. Also, hopefully your native classes don't use multiple inheritance since a managed class can only have one non-interface base class. John