Integrate a .NET Assembly Into a C# Application

This example shows how to call a .NET assembly from a C# application.

  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.

    The assembly is located in the application folder created when you installed the component. For this example, select makeSqr.dll.

  5. Create a reference to the MWArray API.

    The API is located in MATLABROOT\toolbox\dotnetbuilder\bin\arch\version\MWArray.dll.

  6. Make .NET Namespaces available to your generated component and MWArray libraries. Add the following using statements to your C#/.NET code:

    using com.component_name;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;

    In this example name the component demo.

  7. 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;
        }
      }
    }
  8. Instantiate your component class.

    obj = new MLTestClass();
    
  9. Invoke your component. After you complete the tasks of initializing and instantiating the classes you are working with, invoke the makeSqr component. Invoke the component method using a signature containing both the number of output arguments expected and the number of input arguments the MATLAB function requires.

    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.makeSqr(1, input);

    You can also use explicit conversion:

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

  10. The makeSqr method returns an array of MWArrays

    Extract the Magic Square you created from the first indice of result and print the output, as follows:

    output = (MWNumericArray)result[0];
    Console.WriteLine(output);
  11. 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.

    using System;
    using System.Collection.Generic;
    using System.Text;
    
    using com.demo;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;
    
    namespace mainApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          MLTestClass obj = null;
          MWNumericArray input = null;
          MWNumericArray output = null;
          MWArray[] result = null;
    
          try
          {
            obj = new MLTestClass();
    
            input = 5;
            result = obj.makeSqr(1, input);
    
            output = (MWNumericArray)result[0];
            Console.WriteLine(output);
          }
          catch
          {
            throw;
          }
       }
      }
    }
  12. After you finish writing your code, you build and run it with Microsoft Visual Studio.

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?