Integrate a .NET Assembly Into a C# Application

This example shows how to call a .NET assembly from a C# application. To create the .NET assembly from your MATLAB® function, see Create a .NET Assembly.

  1. Install the .NET assembly and the MATLAB Runtime.

    The assembly and the MATLAB Runtime are both installed by the Library Compiler app. The installer is located in the for_redistribution folder of your deployment project. The installer places the .NET assembly on your computer and automatically installs the MATLAB Runtime from the Web, if you do not already have the MATLAB Runtime installed on your system.

    You can also download the MATLAB Runtime installer from http://www.mathworks.com/products/compiler/mcr. The generated shared libraries and support files are located in the MATLAB deployment project's for_testing folder.

  2. Open Microsoft® Visual Studio®.

  3. Create a new project.

    For this example create a C# Console Application called MainApp.

  4. Create a reference to your assembly file MagicSquareComp.dll.

    The assembly is located in the application folder created where you installed the component.

  5. Create a reference to the MWArray API. The location of the API within MATLAB Runtime is:

    C:\Program Files\MATLAB\MATLAB Runtime\v##\toolbox\dotnetbuilder\bin\win64\version\MWArray.dll

  6. Go to Build > Configuration Manager... and change the platform from Any CPU to x64.

  7. Copy the following C# code into the project and save it.

    // Make .NET Namespaces available to your generated component.
    using System;
    
    using MagicSquareComp;
    using MathWorks.MATLAB.NET.Arrays;
    
    
    // Initialize your classes before you use them.
    namespace MainApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          MLTestClass obj = null;
          MWNumericArray input = null;
          MWNumericArray output = null;
          MWArray[] result = null;
    
          // Because class instantiation and method invocation make their exceptions at run time,
          // you should enclose your code in a try-catch block to handle errors.
          try
          {
            // Instantiate your component class.
            obj = new MLTestClass();
    
            // Invoke your component.
            input = 5;
            result = obj.makesquare(1, input);
    
            // Extract the Magic Square you created from the first index of result
            output = (MWNumericArray)result[0];
            
            // print the output.
            Console.WriteLine(output);
          }
          catch
          {
            throw;
          }
       }
      }
    }
  8. After you finish writing your code, build and run it with Microsoft Visual Studio.

Note

When calling your component, you can take advantage of implicit conversion from .NET types to MATLAB types, by passing the native C# value directly to makeSqr:

input = 5;
obj.makesquare(1, input);

You can also use explicit conversion:

input = new MWNumericArray(5);
obj.makesquare(1, input);

Running the Component Installer

The compiler creates an installer for the generated .NET component. After compilation is complete, you can find this installer in the for_redistribution folder in your project folder. By default, the compiler names the installer MyAppInstaller_web.exe or MyAppInstaller_mcr.exe, depending on which packaging option you chose. Using the Application Information area of the Library Compiler app, you can customize the look of the installer.

For example, when an end-user double-clicks the component installer, the first screen identifies you component by name and version number.

By clicking Next on each screen, the installer leads you through the installation process. During installation, you can specify the installation folder. The installer also automatically downloads the MATLAB Runtime, if needed.

Was this topic helpful?