Main Content

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 Generate .NET Assembly and Build .NET Application.

  1. Install the .NET assembly from the for_redistribution folder.

    The generated shared libraries and support files are located in the for_testing folder.

  2. Open Microsoft® Visual Studio® and create a project. For this example, create a C# Console Application called MainApp and create a reference to your assembly file MagicSquareComp.dll.

  3. Add a reference to the MWArray API.

    If MATLAB is installed on your systemmatlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll
    If MATLAB Runtime is installed on your system<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll

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

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

     C# Code to Implement Application

  6. After you finish writing your code, build and run it with Microsoft Visual Studio.


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