views:

435

answers:

1

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++?

+1  A: 

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

PrivateObject privateFoo = gcnew PrivateObject( "CUnmanagedType" )
Jared