我在应用程序的某些点引用的反射辅助类中有一个方法:
public static IEnumerable<TypeInfo> GetControllersWeb()
{
return AppDomain.CurrentDomain
.GetAssemblies()
.First(x => x.FullName.StartsWith("MyProject.Web,", StringComparison.CurrentCultureIgnoreCase))
.DefinedTypes
.Where(x => x.Name.EndsWith("Controller", StringComparison.CurrentCultureIgnoreCase));
}
有人在几天前在项目中做了一个更改(删除了一些类),现在这个方法抛出异常。例外情况如下:
无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息。
在 LoaderExceptions 中,它描述了一个被删除的类。
如果我将linq查询更改为:
AppDomain.CurrentDomain
.GetAssemblies()
.First()
.DefinedTypes;
工作正常。我想知道First()
方法可能会导致什么?也许某些事情打破了项目设置?
更新
检查AppDomain.CurrentDomain.GetAssemblies()
的结果我可以找到所需的程序集(它是索引39):