Contenuto principale

NET.addAssembly

Make .NET assembly visible to MATLAB

Description

asmInfo = NET.addAssembly(globalName) loads a Base Class Library (BCL) or an assembly installed in the Global Assembly Cache (GAC) (.NET Framework only).

example

asmInfo = NET.addAssembly(privateName) loads a private .NET assembly.

asmInfo = NET.addAssembly(___,Unloadable=tf) lets you opt-in to unload a private, .NET Core assembly. To unload an assembly, set the Unloadable parameter to true when you call NET.addAssembly, then call NET.unloadAssembly. (since R2026a)

You cannot unload a .NET Framework assembly. If you modify and rebuild a .NET Framework assembly, you must restart MATLAB® to access the updated code.

example

Examples

collapse all

Display classes and enumerations in the System.Windows.Forms assembly.

Load the assembly.

asm = NET.addAssembly("System.Windows.Forms")
asm = 

  NET.Assembly handle with properties:

    AssemblyHandle
    Classes
    Structures
    Enums
    GenericTypes
    Interfaces
    Delegates

Display the classes in the assembly.

asm.Classes
ans =

  651×1 cell array

    {'System.Resources.ResXDataNode'                                                             }
    {'System.Resources.ResXFileRef'                                                              }
    {'System.Resources.ResXResourceReader'                                                       }
    {'System.Resources.ResXResourceSet'                                                          }
    {'System.Resources.ResXResourceWriter'                                                       }
    {'System.Windows.Forms.ToolStripTextBox'                                                     }
    ...

Display the enumerations.

asm.Enums
ans =

  229×1 cell array

    {'System.Windows.Forms.AccessibleEvents'                            }
    {'System.Windows.Forms.AccessibleNavigation'                        }
    {'System.Windows.Forms.AccessibleRole'                              }
    {'System.Windows.Forms.AccessibleSelection'                         }
    {'System.Windows.Forms.AccessibleStates'                            }
    {'System.Windows.Forms.AnchorStyles'                                }
    ...

First, load the global assembly System.Windows.Forms into MATLAB. Then import and call the System.Windows.Forms.MessageBox.Show method in the global assembly System.Windows.Forms.

asmInfo = NET.addAssembly("System.Windows.Forms");
import System.Windows.Forms.*
MessageBox.Show("Simple Message Box")

Suppose that you have an assembly MyAssembly.dll containing the class MyAssembly.MyClass located at C:\myPath\.

Select .NET Core.

dotnetenv("core");

Add an assembly to MATLAB and enable unloading the assembly.

asm = NET.addAssembly("C:\path\to\MyAssembly.dll",Unloadable=true);

Use some API from the assembly.

MyAssembly.MyClass.SomeStaticMethod;

Unload the assembly from MATLAB.

NET.unloadAssembly("MyAssembly");

Input Arguments

collapse all

Global assembly name, specified as a string scalar, character vector, or System.Reflection.AssemblyName object.

Private assembly name including the full path, specified as a string scalar or character vector. You must specify the full path to the private assembly file. For more information, see Assembly Is a Library of .NET Classes.

Example: "C:\Work\MyProject\MyAssembly.dll"

Since R2026a

Assembly is unloadable, specified as a numeric or logical 0 (false) or 1 (true). When you set Unloadable to true, the function loads the assembly so that it can be unloaded.

Setting Unloadable to true is supported only for private, .NET Core assemblies.

Some assemblies do not support unloading due to how they are compiled. For those assemblies, call NET.addAssembly with Unloadable=false.

Example: asm = NET.addAssembly("C:\path\to\MyAssembly.dll", Unloadable=true);

Output Arguments

collapse all

Assembly information containing names of the members of the assembly, returned as a NET.Assembly object.

Limitations

  • NET.addAssembly does not support assemblies generated by the MATLAB Compiler SDK™ product.

Tips

  • You do not need to call NET.addAssembly to access classes in the mscorlib.dll and system.dll assemblies. MATLAB dynamically loads these assemblies from the .NET class library the first time you type "NET." or "System.".

  • Refer to your .NET product documentation for the name of the assembly and its deployment type (global or private).

Version History

Introduced in R2009a

expand all