简体   繁体   中英

How to convert a user-defined unmanaged type to a managed type?

I have a test that I'm writing in MSTest, which is managed C++, and I'm trying to test an unmanaged class. Specifically, I'm trying to use the PrivateObject class to call a private method.

This is the code that I have so far:

CUnmanagedType foo;
PrivateObject privateFoo = gcnew PrivateObject( foo );

CString strFromFoo = privateFoo.Invoke( "ARandomPrivateMethod" );

When I compile I get an error that foo is not convertable to System::Type^. I've tried doing the following:

PrivateObject privateFoo = gcnew PrivateObject( (gcnew System::Type^(foo)) );

but that won't work because System::Type^ is an abstract type. Any ideas?

I've looked at these questions, but they used pre-defined types, not user-defined ones: How to convert a unmanaged double to a managed string? Conversion between managed and unmanaged types in C++?

The PrivateObject constructor wants a typename, not an instance. To do this, you would need to do the following:

PrivateObject privateFoo = gcnew PrivateObject( "CUnmanagedType" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM