unity 2019.4.40f1 反射 GetCustomAttribute nunit.framework.dll 找不到报错
因为反射的 Type 里面包含了像 JetBrains.Rider 的类型,这类类型获取 attribute 的时候需要找高版本的 nunit.framework 导致报错,故遍历程序集 Type 时要过滤掉这些 Type
public static List<Type> SafeGetTypes()
var type_list = new List<Type>();
var asm_list = AppDomain.CurrentDomain.GetAssemblies();
foreach (var asm in asm_list)
type_list.AddRange(asm.GetTypes()
.Where( x =>
var nameSpace = x.Namespace;
return nameSpace == null
// JetBrains.Rider 的类型引用高版本 nunit,导致获取 attribute 时报错
|| !nameSpace.StartsWith("JetBrains.Rider", StringComparison.Ordinal);
catch (ReflectionTypeLoadException ex)
var sb = new StringBuilder();
sb.AppendFormat("加载类型失败! asm:{0}\n", asm);
foreach (var e2 in ex.LoaderExceptions)
sb.AppendFormat(" loader 错误: {0}\n", e2.Message);
if (e2 is FileNotFoundException e3)
sb.AppendFormat(" Fusion Log: {0}\n", e3.FusionLog);
Debug.LogError(sb.ToString());
return type_list;
---------- 旧答案 -------------------
FileNotFoundException: Could not load file or assembly 'nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb' or one of its dependencies.
错误详情:
FileNotFoundException: Could not load file or assembly 'nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb' or one of its dependencies.
System.Reflection.MonoMethodInfo.GetMethodInfo (System.IntPtr handle) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MonoMethod.GetPseudoCustomAttributes () (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.MonoCustomAttrs.GetPseudoCustomAttributes (System.Reflection.ICustomAttributeProvider obj, System.Type attributeType) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.MonoCustomAttrs.GetCustomAttributesBase (System.Reflection.ICustomAttributeProvider obj, System.Type attributeType, System.Boolean inheritedOnly) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.MonoCustomAttrs.GetCustomAttributes (System.Reflection.ICustomAttributeProvider obj, System.Type attributeType, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MonoMethod.GetCustomAttributes (System.Type attributeType, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, System.Type type, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Attribute.GetCustomAttribute (System.Reflection.MemberInfo element, System.Type attributeType, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.CustomAttributeExtensions.GetCustomAttribute (System.Reflection.MemberInfo element, System.Type attributeType, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T] (System.Reflection.MemberInfo element, System.Boolean inherit) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
xuetaolu.MyToolsWindow+<>c.<GetTools>b__9_3 (<>f__AnonymousType11`2[<type>j__TPar,<info>j__TPar] <>h__TransparentIdentifier0) (at Assets/lib/xuetaolu/editor/windows/MyToolsWindow.cs:124)
System.Linq.Enumerable+SelectEnumerableIterator`2[TSource,TResult].MoveNext () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[TSource,TResult].ToArray () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Buffer`1[TElement]..ctor (System.Collections.Generic.IEnumerable`1[T] source) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.OrderedEnumerable`1+<GetEnumerator>d__3[TElement].MoveNext () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Enumerable+SelectIPartitionIterator`2[TSource,TResult].ToList () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
xuetaolu.MyToolsWindow.GetTools () (at Assets/lib/xuetaolu/editor/windows/MyToolsWindow.cs:121)
xuetaolu.MyToolsWindow.OnGUI () (at Assets/lib/xuetaolu/editor/windows/MyToolsWindow.cs:72)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <7105be432fb64891b07085914e6cd5c1>:0)