Add Unhandled Exception handler for Dlog output
authorpius.lee <pius.lee@samsung.com>
Tue, 15 Nov 2016 06:49:52 +0000 (15:49 +0900)
committerpius.lee <pius.lee@samsung.com>
Tue, 15 Nov 2016 06:52:17 +0000 (15:52 +0900)
Add temporarily implmentation until become Appdomain on API

Change-Id: I6c622d701d94cf3cc29262e066a490f102cfd46b

Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyManager.cs

index 39ff2fa..acbb06e 100644 (file)
@@ -78,10 +78,33 @@ namespace Tizen.Runtime.Coreclr
             }
         }
 
+        public static void UnhandledExceptionHandler(object sender, object args)
+        {
+            TypeInfo unhandledExceptionEventArgsType =
+                Type.GetType("UnhandledExceptionEventArgs").GetTypeInfo();
+
+            PropertyInfo exception = unhandledExceptionEventArgsType.GetProperty("ExceptionObject");
+            Exception e = (Exception)exception.GetValue(args);
+
+            PrintException(e);
+        }
+
         public static bool Initialize(string preloadDirectory)
         {
             try
             {
+                // Set UnhandledException handler with reflection
+                // we must replace this to no reflection method after AppDomain is comming in used net standard
+                TypeInfo appdomainType = Type.GetType("System.AppDomain").GetTypeInfo();
+                PropertyInfo currentDomain = appdomainType.GetProperty("CurrentDomain",
+                        BindingFlags.Public | BindingFlags.Static);
+                EventInfo unhandledException = appdomainType.GetDeclaredEvent("UnhandledException");
+                object appdomain = currentDomain.GetValue(null, null);
+                MethodInfo handlerInfo = typeof(AssemblyManager).GetTypeInfo().GetDeclaredMethod("UnhandledExceptionHandler");
+                Delegate handler = handlerInfo.CreateDelegate(unhandledException.EventHandlerType);
+                var addMethod = unhandledException.GetAddMethod(true);
+                addMethod.Invoke(appdomain, new[] {handler});
+
                 CurrentAssemblyLoaderContext = new AssemblyLoader();
 
                 if (!string.IsNullOrEmpty(preloadDirectory))