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

조회 수: 8 (최근 30일)

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?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2024년 2월 27일
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.")

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Microsoft .NET에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by