Type-Safe Interfaces: An Alternative to MWArray

The MATLAB® data types are incompatible with native .NET types. To send data between your application and .NET, you perform these tasks:

  1. Marshal data from .NET input data to a deployed function by creating an MWArray object from native .NET data. The public functions in a deployed component return MWArray objects.

  2. Marshal the output MATLAB data in an MWArray into native .NET data by calling one of the MWArray marshaling methods (ToArray(), for example).

Manual Data Marshaling Without a Type-Safe Interface

As you can see, manually marshaling data adds complexity and potential failure points to the task of integrating deployed components into a .NET application. This is particularly true for these reasons:

  • Your application cannot detect type mismatch errors until run time. For example, you might accidentally create an MWArray from a string and pass the array to a deployed function that expects a number. Because the wrapper code generated by MATLAB Compiler SDK™ expects an MWArray, the .NET compiler is unable to detect this error and the deployed function either throws an exception or returns the wrong answer.

  • Your end users must learn how to use the MWArray data type or alternately mask the MWArray data type behind a manually written (and manually maintained) API. This introduces unwanted training time and places resource demands on a potentially overcommitted staff.

You can avoid performing MWArray data marshaling by using type-safe interfaces. Such interfaces minimize explicit type conversions by hiding the MWArray type from the calling application. Using type-safe interfaces allows .NET developers to work directly with familiar native data types. For more information, see Implement a Type-Safe Interface.

Simplified Data Marshaling With a Type-Safe Interface