다음을 통해 공유


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.

Remarks

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. For more information, see Breaking Changes in the Visual C++ 2005 Compiler. 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 The #using Directive.

Example

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

See Also

Concepts

Type and Member Visibility