2 * Copyright (c) 2016 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 System.Runtime.InteropServices;
24 namespace Tizen.Runtime.Coreclr
26 public static class AssemblyManager
28 public static bool Launch(
32 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]
35 ALog.Debug($"Application Launch path : {path}");
38 DirectoryInfo bindir = new DirectoryInfo(Path.Combine(rootPath, "bin"));
39 DirectoryInfo libdir = new DirectoryInfo(Path.Combine(rootPath, "lib"));
40 if (Directory.Exists(bindir.FullName))
42 CurrentAssemblyLoaderContext.AddSearchableDirectory(bindir.FullName);
44 if (Directory.Exists(libdir.FullName))
46 CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName);
52 ALog.Debug("Exception in Launch()");
60 public static void Prepared()
66 ALog.Debug($"Failed to Initialized");
71 ALog.Debug("Exception at Preparing");
76 private static void PrintException(Exception exception)
78 while (exception != null)
80 ALog.Debug(exception.ToString());
81 exception = exception.InnerException;
85 public static void UnhandledExceptionHandler(object sender, object args)
87 TypeInfo unhandledExceptionEventArgsType =
88 Type.GetType("UnhandledExceptionEventArgs").GetTypeInfo();
90 PropertyInfo exception = unhandledExceptionEventArgsType.GetProperty("ExceptionObject");
91 Exception e = (Exception)exception.GetValue(args);
96 public static bool Initialize()
100 // Set UnhandledException handler with reflection
101 // we must replace this to no reflection method after AppDomain is comming in used net standard
102 TypeInfo appdomainType = Type.GetType("System.AppDomain").GetTypeInfo();
103 PropertyInfo currentDomain = appdomainType.GetProperty("CurrentDomain",
104 BindingFlags.Public | BindingFlags.Static);
105 EventInfo unhandledException = appdomainType.GetDeclaredEvent("UnhandledException");
106 object appdomain = currentDomain.GetValue(null, null);
107 MethodInfo handlerInfo = typeof(AssemblyManager).GetTypeInfo().GetDeclaredMethod("UnhandledExceptionHandler");
108 Delegate handler = handlerInfo.CreateDelegate(unhandledException.EventHandlerType);
109 var addMethod = unhandledException.GetAddMethod(true);
110 addMethod.Invoke(appdomain, new[] {handler});
114 ALog.Debug("Exception on set handler for unhandled exception");
120 CurrentAssemblyLoaderContext = new AssemblyLoader();
124 ALog.Debug("Exception on Initialized");
131 public static void Execute(string dllPath, string[] argv)
135 FileInfo f = new FileInfo(dllPath);
136 if (File.Exists(f.FullName))
138 Assembly asm = CurrentAssemblyLoaderContext.LoadFromPath(f.FullName);
140 if (asm == null) throw new FileNotFoundException($"{f.FullName} is not found");
141 if (asm.EntryPoint == null) throw new ArgumentException($"{f.FullName} did not have EntryPoint");
142 asm.EntryPoint.Invoke(null, new object[]{argv});
146 ALog.Debug($"Requested file does not exist: {dllPath}");
151 ALog.Debug("Exception on Execute");
156 public static AssemblyLoader CurrentAssemblyLoaderContext