How can I pass gcroot <Object ^> to IMyInterface in C ++. Net?

I need to do some strange things with gcroot, but I get the following error in the dynamic line: “cannot use dynamic_cast” to convert from “gcroot” to “IMyInterface ^". In C #, you could easily dump a shared object to any interface: You may receive a runtime error if the object does not implement the interface, but it will be compiled.

gcroot<Object^> m_pDataObject;
IMyInterface obj = dynamic_cast<IMyInterface^>(m_pDataObject);
+5
source share
2 answers

This works (compiles) and should do what you want (the module replaces IDisposablewith your required interface):

gcroot<Object^> m_pDataObject;
Object^ obj = m_pDataObject;     // implicit conversion from gcroot<>
IDisposable^ intf = dynamic_cast<IDisposable^>(obj);    // or safe_cast<>
+9

gcroot < > - . , :

IMyInterface^ itf = dynamic_cast<IMyInterface^>((Object^)m_pDataObject);

.

+5

All Articles