Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type Visibility

You can control the visibility of CLR types, such that, if an assembly is referenced, types in the assembly can be visible or not visible outside the assembly.

public indicates that a type will be visible to any source files that contain a #using directive for its containing assembly. private indicates that a type will not be visible to any source files that contain a #using directive for its containing assembly. However, private types are visible within the same assembly. The default visibility for a class is private.

Prior to Visual C++ 2005, native types had public accessibility by default outside the assembly. Enable Compiler Warning (level 1) C4692 to help you see where you are using private native types incorrectly. Use the make_public pragma to give a public accessibility to a native type in a source code file you cannot modify.

For more information, see #using Directive (C/C++).

The following sample shows how to declare types specifying their accessibility, and then access those types inside the assembly. However, if a component with private types was referenced with #using, only public types in the assembly would be visible.

// type_visibility.cpp
// compile with: /clr
using namespace System;
// public type, visible inside and outside assembly
public ref struct Public_Class {
   void Test(){Console::WriteLine("in Public_Class");}
};

// private type, visible inside but not outside assembly
private ref struct Private_Class {
   void Test(){Console::WriteLine("in Private_Class");}
};

// default accessibility is private
ref class Private_Class_2 {
public:
   void Test(){Console::WriteLine("in Private_Class_2");}
};

int main() {
   Public_Class ^ a = gcnew Public_Class;
   a->Test();

   Private_Class ^ b = gcnew Private_Class;
   b->Test();

   Private_Class_2 ^ c = gcnew Private_Class_2;
   c->Test();
}
in Public_Class
in Private_Class
in Private_Class_2

Now, let's rewrite the previous sample so that it is built as a DLL.

// type_visibility_2.cpp
// compile with: /clr /LD
using namespace System;
// public type, visible inside and outside assembly
public ref struct Public_Class {
   void Test(){Console::WriteLine("in Public_Class");}
};

// private type, visible inside but not outside assembly
private ref struct Private_Class {
   void Test(){Console::WriteLine("in Private_Class");}
};

// default accessibility is private
ref class Private_Class_2 {
public:
   void Test(){Console::WriteLine("in Private_Class_2");}
};

This client consumes the component built in the previous sample showing how to access types outside the assembly.

// type_visibility_3.cpp
// compile with: /clr
#using "type_visibility_2.dll"
int main() {
   Public_Class ^ a = gcnew Public_Class;
   a->Test();

   // private types not accessible outside assembly
   // Private_Class ^ b = gcnew Private_Class;
   // Private_Class_2 ^ c = gcnew Private_Class_2;
}
in Public_Class
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.