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 AssemblyLoader CurrentAssemblyLoaderContext
34 public static bool Launch(
38 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
41 ALog.Debug($"Application Launch path : {path}");
44 DirectoryInfo binDir = new DirectoryInfo(Path.Combine(rootPath, "bin"));
45 DirectoryInfo libDir = new DirectoryInfo(Path.Combine(rootPath, "lib"));
46 if (Directory.Exists(binDir.FullName))
48 CurrentAssemblyLoaderContext.AddSearchableDirectory(binDir.FullName);
51 if (Directory.Exists(libDir.FullName))
53 CurrentAssemblyLoaderContext.AddSearchableDirectory(libDir.FullName);
60 ALog.Debug("Exception in Launch()");
68 public static void Prepared()
74 ALog.Debug($"Failed to Initialized");
79 ALog.Debug("Exception at Preparing");
84 public static void UnhandledExceptionHandler(object sender, object args)
86 TypeInfo unhandledExceptionEventArgsType =
87 Type.GetType("UnhandledExceptionEventArgs").GetTypeInfo();
89 PropertyInfo exception = unhandledExceptionEventArgsType.GetProperty("ExceptionObject");
90 Exception e = (Exception)exception.GetValue(args);
95 public static bool Initialize()
99 /// Set UnhandledException handler with reflection
100 /// we must replace this to no reflection method after AppDomain is comming in used net standard
101 TypeInfo appdomainType = Type.GetType("System.AppDomain").GetTypeInfo();
102 PropertyInfo currentDomain = appdomainType.GetProperty("CurrentDomain",
103 BindingFlags.Public | BindingFlags.Static);
104 EventInfo unhandledException = appdomainType.GetDeclaredEvent("UnhandledException");
105 object appDomain = currentDomain.GetValue(null, null);
106 MethodInfo handlerInfo = typeof(AssemblyManager).GetTypeInfo().GetDeclaredMethod("UnhandledExceptionHandler");
107 Delegate handler = handlerInfo.CreateDelegate(unhandledException.EventHandlerType);
108 var addMethod = unhandledException.GetAddMethod(true);
109 addMethod.Invoke(appDomain, new[] {handler});
113 ALog.Debug("Exception on set handler for unhandled exception");
119 CurrentAssemblyLoaderContext = new AssemblyLoader();
123 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);
142 throw new FileNotFoundException($"{f.FullName} is not found");
145 if (asm.EntryPoint == null)
147 throw new ArgumentException($"{f.FullName} did not have EntryPoint");
150 asm.EntryPoint.Invoke(null, new object[] {argv});
154 ALog.Debug($"Requested file does not exist: {dllPath}");
159 ALog.Debug("Exception on Execute");
164 private static void PrintException(Exception exception)
166 while (exception != null)
168 ALog.Debug(exception.ToString());
169 exception = exception.InnerException;