NewLife/X

屏蔽判定Unity时抛出的异常
智能大石头 authored at 2025-03-14 00:52:58
16cb053
Tree
1 Parent(s) 80cebb4
Summary: 2 changed files with 22 additions and 3 deletions.
Modified +18 -2
Modified +4 -1
Modified +18 -2
diff --git a/NewLife.Core/Common/Runtime.cs b/NewLife.Core/Common/Runtime.cs
index f4b997f..c5c6121 100644
--- a/NewLife.Core/Common/Runtime.cs
+++ b/NewLife.Core/Common/Runtime.cs
@@ -13,6 +13,22 @@ namespace NewLife;
 /// </remarks>
 public static class Runtime
 {
+    #region 静态构造
+    static Runtime()
+    {
+        try
+        {
+            Mono = Type.GetType("Mono.Runtime") != null;
+        }
+        catch { }
+        try
+        {
+            Unity = Type.GetType("UnityEngine.Application, UnityEngine") != null;
+        }
+        catch { }
+    }
+    #endregion
+
     #region 控制台
     private static Boolean? _IsConsole;
     /// <summary>是否控制台。用于判断是否可以执行一些控制台操作。</summary>
@@ -49,10 +65,10 @@ public static class Runtime
 
     #region 系统特性
     /// <summary>是否Mono环境</summary>
-    public static Boolean Mono { get; } = Type.GetType("Mono.Runtime") != null;
+    public static Boolean Mono { get; }
 
     /// <summary>是否Unity环境</summary>
-    public static Boolean Unity { get; } = Type.GetType("UnityEngine.Application, UnityEngine") != null;
+    public static Boolean Unity { get; }
 
 #if !NETFRAMEWORK
     private static Boolean? _IsWeb;
Modified +4 -1
diff --git a/Test/Program.cs b/Test/Program.cs
index 0486ee3..28a0f13 100644
--- a/Test/Program.cs
+++ b/Test/Program.cs
@@ -77,7 +77,7 @@ public class Program
             try
             {
 #endif
-                Test4();
+            Test1();
 #if !DEBUG
             }
             catch (Exception ex)
@@ -99,6 +99,9 @@ public class Program
     static StarClient _client;
     private static void Test1()
     {
+        var type = Type.GetType("UnityEngine.Application, UnityEngine");
+        XTrace.WriteLine("UnityEngine:{0}", type?.FullName);
+
         var pool = Pool.Shared;
         var buf = pool.Rent(1000);