How should I add .NET assemblies and import libraries for a singleton class?

8 views (last 30 days)

I have some .NET object that I would like to be reused, so I created a singleton class. My singleton class depends on imported libraries from a .NET assembly. How should I add the assembly and import the libraries?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Feb 2024
It is best to add the assemblies and import the libraries in the constructor, then the namespaces are imported after the constructor is executed. After that, you can work with the .NET classes from the MATLAB class properties or methods. You can also refer to it from another script since imports are global.
Below is an example of a singleton class that depends on the "System.Windows.Forms" assembly and libraries. These are added in the constructor, which is only executed once.
classdef messageBoxSingleton < handle
    properties (Access = private)
        messageBox;
        message;
    end
    methods (Access = public)
        function obj = messageBoxSingleton
            % Private constructor to prevent external instantiation
            % Add assemblies and import libraries here
            NET.addAssembly('System.Windows.Forms');
            import System.Windows.Forms.*;
        end
    end
    methods (Static)
        function obj = getInstance
            % Returns the single instance of the class
            persistent singletonInstance
            if isempty(singletonInstance) || ~isvalid(singletonInstance)
                singletonInstance = messageBoxSingleton();
                obj = singletonInstance;
                setDefault(obj);
            else
                obj = singletonInstance;
            end
        end
    end
    methods
        function setDefault(obj)
            obj.messageBox = System.Windows.Forms.MessageBox();
            obj.message = "Default message";
        end
        function setMessage(obj,mess)
            obj.message = mess;
        end
        function showMessage(obj)
            obj.messageBox.Show(obj.message);
        end
    end
        
end
This code shows how the singleton class can be used in MATLAB. The last statement does not use the singleton class and illustrates that the assembly is loaded globally.
% Use MessageBox from the singleton class
m1 = messageBoxSingleton.getInstance(); % Assembly added and libraries imported here
m1.showMessage(); % shows "Default message"
% Now change the message
m1.setMessage("Message 1");
m1.showMessage(); % shows new "Message 1"
% New object from the singleton class
m2 = messageBoxSingleton.getInstance();
m2.showMessage(); % shows same message as m1 - "Message 1"
% Use MessageBox without the Singleton class
% Assemblies and libraries are already loaded
System.Windows.Forms.MessageBox.Show("Simple message box.")

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!