MATLAB: Can I generate a .NET dll that imports other .NET assembly

dllMATLABMATLAB CompilerMATLAB Compiler SDKnet

Hi!,
This might sound weird, but I want to use a .NET assembly on Matlab (I already know how to do this), make some functions with it, and then generate a .NET .dll with the compile tool. My question is, will the compile tool complain about the usage of external code?
The situation is as follows: I was given a C# programmed API, which I have to implement. I have to create other functions, which consume the API. I would like to make these other functions in Matlab, since I have much more experience there than in C#. The thing is that these functions have to be exported to another program, via dll (this is not optional). So, I was thinking to compile these new functions into a new .NET assembly.
Thanks a lot in advance!

Best Answer

Yes, you can use external .NET dll in compiled MATLAB function.
For example, assume the name of external dll is 'dotNetExternalClass.dll'. In MATLAB function, you can load that dll with full path.
function out = compileOtherDll(in1, in2)
externalDllPath = fullfile(pwd, 'dotNetExternalClass.dll');
asm = NET.addAssembly(externalDllPath);
% do something
% ...

% ...
end
Then, compile this function as .NET assembly using deploytool. compileOtherDll.dll will be created in this case.
After that, in Visual Studio, it's needed to add compileOtherDll.dll and $MATLAB_INSTALL\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll as references and add "using" in C# codes.
using MathWorks.MATLAB.NET.Arrays;
using compileOtherDll;
After building this C# codes, you need to place the external dll (dotNetExternalClass.dll) in the same folder of exe file.