NewLife/X

CustomAttributeData可能会导致只反射加载,需要屏蔽内部异常
大石头 authored at 2017-08-02 14:34:27
bae7d01
Tree
1 Parent(s) 62ac668
Summary: 2 changed files with 17 additions and 8 deletions.
Modified +5 -1
Modified +12 -7
Modified +5 -1
diff --git a/NewLife.Core/Reflection/AssemblyX.cs b/NewLife.Core/Reflection/AssemblyX.cs
index b0e5cfb..63e56c3 100644
--- a/NewLife.Core/Reflection/AssemblyX.cs
+++ b/NewLife.Core/Reflection/AssemblyX.cs
@@ -131,7 +131,11 @@ namespace NewLife.Reflection
         static AssemblyX()
         {
 #if !__MOBILE__ && !__CORE__
-            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (sender, args) => Assembly.ReflectionOnlyLoad(args.Name);
+            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (sender, args) =>
+            {
+                XTrace.WriteLine("[{0}]请求只反射加载[{1}]", args.RequestingAssembly?.FullName, args.Name);
+                return Assembly.ReflectionOnlyLoad(args.Name);
+            };
 #endif
         }
         #endregion
Modified +12 -7
diff --git a/NewLife.Core/Reflection/AttributeX.cs b/NewLife.Core/Reflection/AttributeX.cs
index 1f11e20..d2768b4 100644
--- a/NewLife.Core/Reflection/AttributeX.cs
+++ b/NewLife.Core/Reflection/AttributeX.cs
@@ -122,16 +122,21 @@ namespace System
         {
             if (target == null) return default(TResult);
 
-            var list = CustomAttributeData.GetCustomAttributes(target);
-            if (list == null || list.Count < 1) return default(TResult);
-
-            foreach (var item in list)
+            // CustomAttributeData可能会导致只反射加载,需要屏蔽内部异常
+            try
             {
-                if (typeof(TAttribute) != item.Constructor.DeclaringType) continue;
+                var list = CustomAttributeData.GetCustomAttributes(target);
+                if (list == null || list.Count < 1) return default(TResult);
 
-                var args = item.ConstructorArguments;
-                if (args != null && args.Count > 0) return (TResult)args[0].Value;
+                foreach (var item in list)
+                {
+                    if (typeof(TAttribute) != item.Constructor.DeclaringType) continue;
+
+                    var args = item.ConstructorArguments;
+                    if (args != null && args.Count > 0) return (TResult)args[0].Value;
+                }
             }
+            catch { }
 
             return default(TResult);
         }