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;
24 internal class NUIGadgetAssemblyLoadContext : AssemblyLoadContext
26 public NUIGadgetAssemblyLoadContext() : base(isCollectible: true)
30 protected override Assembly Load(AssemblyName name)
36 internal class NUIGadgetAssembly
38 private static readonly object _assemblyLock = new object();
39 private readonly string _assemblyPath;
40 private WeakReference _assemblyRef;
41 private Assembly _assembly = null;
43 public NUIGadgetAssembly(string assemblyPath) { _assemblyPath = assemblyPath; }
49 if (_assembly != null)
54 Log.Warn("Load(): " + _assemblyPath + " ++");
55 NUIGadgetAssemblyLoadContext context = new NUIGadgetAssemblyLoadContext();
56 _assemblyRef = new WeakReference(context);
57 using (MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(_assemblyPath)))
59 _assembly = context.LoadFromStream(memoryStream);
61 Log.Warn("Load(): " + _assemblyPath + " --");
65 public bool IsLoaded { get { return _assembly != null; } }
67 public NUIGadget CreateInstance(string className)
71 return (NUIGadget)_assembly?.CreateInstance(className);
79 if (_assembly == null)
84 Log.Warn("Unload(): " + _assemblyPath + " ++");
85 if (_assemblyRef.IsAlive)
87 (_assemblyRef.Target as NUIGadgetAssemblyLoadContext).Unload();
91 Log.Warn("Unload(): " + _assemblyPath + " --");