C++/CLI: Native types visibility/access

Say we have the following code in a C++/CLI assembly:

//Assembly A
public ref class ManagedClass{
public:
    ManagedClass( const NativeClass &a );
}

If we wish to invoke this constructor from another C++/CLI assembly, as in this case...

//Assembly B
ManagedClass^ AnotherManagedClass::Property::get(){
    NativeClass temp;
    return gcnew ManagedClass( temp );
}

... when we try to compile Assembly B, we'll get a compile error saying that the constructor we're trying to call is private. But how is that possible, if we declared it public? The answer is here.

To sum it up, the workaround is using the #pragma make_public(NativeClass) directive right below the #include for NativeClass. This makes NativeClass accesible to other C++/CLI assemblies.

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x