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

JavaScript EditorDhtml editor     Free javascript download 



Main Page

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).

Example

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

В CopyCode imageCopy Code
// 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() {
      System::Console::WriteLine(i);
   }
};

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.

В CopyCode imageCopy Code
// 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;
   x.Test();

   System::Console::WriteLine(NativeClass::Test());   // C2653
}

See Also



JavaScript EditorDhtml editor     Free javascript download