2 * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Reflection;
20 using System.Runtime.Loader;
22 using SystemIO = System.IO;
26 internal class NUIGadgetAssemblyLoadContext : AssemblyLoadContext
28 public NUIGadgetAssemblyLoadContext() : base(isCollectible: true)
32 protected override Assembly Load(AssemblyName name)
38 internal class NUIGadgetAssembly
40 private static readonly object _assemblyLock = new object();
41 private readonly string _assemblyPath;
42 private WeakReference _assemblyRef;
43 private Assembly _assembly = null;
45 public NUIGadgetAssembly(string assemblyPath) { _assemblyPath = assemblyPath; }
51 if (_assembly != null)
56 Log.Warn("Load(): " + _assemblyPath + " ++");
57 NUIGadgetAssemblyLoadContext context = new NUIGadgetAssemblyLoadContext();
58 _assemblyRef = new WeakReference(context);
59 string directoryPath = SystemIO.Path.GetDirectoryName(_assemblyPath);
60 string fileName = SystemIO.Path.GetFileNameWithoutExtension(_assemblyPath);
61 string nativeImagePath = directoryPath + "/" + fileName + ".ni.dll";
62 Log.Debug("NativeImagePath=" + nativeImagePath + ", AssemblyPath=" + _assemblyPath);
63 _assembly = context.LoadFromNativeImagePath(nativeImagePath, _assemblyPath);
64 Log.Warn("Load(): " + _assemblyPath + " --");
68 public bool IsLoaded { get { return _assembly != null; } }
70 public NUIGadget CreateInstance(string className)
74 return (NUIGadget)_assembly?.CreateInstance(className);
82 if (_assembly == null)
87 Log.Warn("Unload(): " + _assemblyPath + " ++");
88 if (_assemblyRef.IsAlive)
90 (_assemblyRef.Target as NUIGadgetAssemblyLoadContext).Unload();
94 Log.Warn("Unload(): " + _assemblyPath + " --");