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

How to: Use a Native Type in a /clr Compilation

You can define a native type in a /clr compilation and any use of that native type from within the assembly is valid. However, native types will not be available for use from referenced metadata.

Each assembly must contain the definition of every native type it will use.

For more information, see /clr (Common Language Runtime Compilation).

This sample creates a component that defines and uses a native type.

// use_native_type_in_clr.cpp
// compile with: /clr /LD
public struct NativeClass {
   static int Test() { return 98; }

public ref struct ManagedClass {
   static int i = NativeClass::Test();
   void Test() {

This sample defines a client that consumes the component. Notice that it is an error to access the native type, unless it is defined in the compiland.

// use_native_type_in_clr_2.cpp
// compile with: /clr
#using "use_native_type_in_clr.dll"
// Uncomment the following 3 lines to resolve.
// public struct NativeClass {
//    static int Test() { return 98; }
// };

int main() {
   ManagedClass x;

   System::Console::WriteLine(NativeClass::Test());   // C2653
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.