The scenario I’m working with looks like this:
public interface INativeWindow { ... }
is a native window type of interface holding a few required, basic methods to implement in order to have a minimal window support (e.g. minimize, restore, etc.). This is a C# interface belonging to an interfaces project.
Now I want to implement it in a CLINativeWindow c++-cli class:
public ref class CLINativeWindow: public SWF::NativeWindow, INativeWindow
{
...
}
Lastly, I have another ref c++ cli class that has an internal member of the CLINativeWindow type and a wrapping property that returns an INativeWindow^ handle:
virtual property INativeWindow^ OwnNativeWindow
{
INativeWindow^ __clrcall get() sealed { return NativeWindow;}
void __clrcall set(INativeWindow^ value) { NativeWindow = dynamic_cast<CLINativeWindow^>(value);}
}
The issue here is that the dynamic_cast doesn’t work, nor does the implicit polymorphic downcast from the property’s getter method. If I’m not misinformed, I think I read in several places that in C++ with normal pointers, such a blunder really works. Now as an SWF::NativeWindow is the classic windows forms native window class (sorry about the unintended alliteration), it should have a virtual method, just as much as the interface does, so no problem for the dynamic cast operator since a polymorphic inheritance is involved. Am I wrong or is this just plain impossible in dotNet C++ CLI?
EDIT
additional code:
public ref class ExampleForSO
{
CLINativeWindow^ NativeWindow;
virtual property INativeWindow^ OwnNativeWindow
{
INativeWindow^ __clrcall get() sealed { return NativeWindow;}
void __clrcall set(INativeWindow^ value) { NativeWindow = dynamic_c
ast<CLINativeWindow^>(value);}
}
}
The errors are from compile time and they state that the conversion/cast cannot be performed for both getter and setter (i.e. cannot convert from B^ to A^ and from B^ to A^)..
UPDATE
If the classes are in different files, implementing the property in a header won’t do. Implementing it in the separate .cpp source file does not cause a compile time error any more and works as desired.
There is no multiple inheritance here. Your
CLINativeWindowis inheriting from one class:SWF::NativeWindow, and it is also implementingINativeWindowinterface.You did not provide enough information (what errors are you getting? sample code that can be pasted in VS to reproduce the problem?), but in general there is nothing wrong with the code that you posted.
My guess is that your
NativeWindowis declared as:without the much needed
^. Try declaring it as:EDIT
The following compiles cleanly for me: