views:

21

answers:

2

I'm having to do some weird things with gcroot, but I get the following error on the dynamic cast line: "cannot use 'dynamic_cast' to convert from 'gcroot' to 'IMyInterface^'. In C#, you could easily cast a generic object to any interface. You may get a runtime error if the object doesn't implement the interface but it would compile.

gcroot<Object^> m_pDataObject;
IMyInterface obj = dynamic_cast<IMyInterface^>(m_pDataObject);
+2  A: 

This works (compiles) and should do what you want (module replacing IDisposable with your required interface):

gcroot<Object^> m_pDataObject;
Object^ obj = m_pDataObject;     // implicit conversion from gcroot<>
IDisposable^ intf = dynamic_cast<IDisposable^>(obj);    // or safe_cast<>
Steve Townsend
That seems to work. I was missing the implicit conversion.
bsh152s
@bsh152s - great, thanks for the followup
Steve Townsend
+1  A: 

gcroot<> is a smart pointer. You can cast to get the tracking handle out of it:

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

Steve's answer is fine too btw.

Hans Passant