从程序集加载类型,遇到 ReflectionTypeLoadException 的处理办法
处理办法
catch ReflectionTypeLoadException ,然后从里面读取 Types 数据(成功加载的类型)就可以了。
参考
ReflectionTypeLoadException Class (System.Reflection) | Microsoft Docs
.net - How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes() - Stack Overflow
代码封装
class AssemblyTypesDetector
{
public Assembly Assembly { get; }
public Exception Exception { get; private set; }
public AssemblyTypesDetector(Assembly assembly)
{
Assembly = assembly;
}
public AssemblyTypesDetector(string filePath)
{
Assembly = Assembly.LoadFile(filePath);
}
public AssemblyTypesDetector(AssemblyName assemblyName)
{
Assembly = Assembly.Load(assemblyName);
}
public IList<Type> DetectTypes()
{
Type[] types = null;
try
{
types = Assembly.GetTypes().ToArray();
}
catch (ReflectionTypeLoadException reflectionTypeLoadException)
{
types = reflectionTypeLoadException.Types.Where(t => t != null).ToArray();
}
catch (Exception ex)
{
Exception = ex;
}
return types?.ToList() ?? new List<Type>();
}
public IList<Type> DetectTypes(Predicate<Type> predicate)
{
return DetectTypes().Where(predicate.Invoke).ToList();
}
}
原文链接:
https://www.cnblogs.com/jasongrass/p/11990633.html
从程序集加载类型,遇到 ReflectionTypeLoadException 的处理办法的更多相关文章
- 未能加载文件或程序集 system.Web.Http.WebHost解决办法。
在csdn中找到一个方法: Update-Package Microsoft.AspNet.WebApi -reinstall 然后就好了. 另外一个方法是缺少哪个dll,就复制一个dll放到bin文 ...
- 一次修改闭源 Entity Provider 程序集以兼容新 EntityFramework 的过程
读完本文你会知道,如何在没有源码的情况下,直接修改一个 DLL 以去除 DLL 上的强命名限制,并在该程序集上直接添加你的“友元程序集(一种特殊的 Attribute,将它应用在程序集上,使得程序集内 ...
- 【ASP.NET MVC】"[A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 ..."的解决办法
1.错误页面: “/”应用程序中的服务器错误. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 [B]System.Web ...
- C# 引用的程序集没有强名称
首先查一下什么是强名称程序集,见百度百科帖子:http://baike.baidu.com/view/1145682.htm简单来说,就是为了解决Windows Dll Hell问题的,即不同公司开发 ...
- C# Winform 未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项。试图加载格式不正确的程序
在使用Winform 开发了一个小软件,其中使用了SQLite作为数据库 但在我的Win7 64位系统上却出现了以下错误: System.BadImageFormatException: 未能加载文件 ...
- 2019-1-17 前言 C#高级编程(第11版)
C#已更新为更快的速度.主要版本7.0是2017年3月发布,次要版本7.1和7.2很快发布在2017年8月和2017年12月.通过项目设置,您可以与每个应用程序一起分发,是开源的,不可用仅适用于Win ...
- c# dll问题
问题描述: dll完全拷贝另一个程序,可是报缺少引用程序集之类的错误. 解决办法: 有可能是.net版本造成的错误.一般常见在3.5升到4之后,存在很多容差.
- 关于 RuntimeBinderException 异常
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException的异常一般来自于两种: 第一种情况: Predefined type 'Microsoft.C ...
- 配置IIS提示打开目录浏览时的问题:未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的解决办法
错误消息: 未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的 ...
随机推荐
- OpenWrite插件上架Google商店,插件安装更加容易!用户安全更有保障!
随着越来越多用户加入OpenWrite写作与快速发布文章到各大技术社区,不少非程序员童鞋经常会卡在插件安装这一步. 这是因为之前我们没有把插件上架到Google商店,所以需要用比较硬核的方式来安装. ...
- Customize the Application UI and Behavior 自定义应用程序UI和行为
In XAF, the business model defines the database structure and UI appearance. Changes to your persist ...
- Python3爬取豆瓣网电影信息
# -*- coding:utf-8 -*- """ 一个简单的Python爬虫, 用于抓取豆瓣电影Top前250的电影的名称 Language: Python3.6 ...
- SQL注入:POST注入
POST注入简介 POST注入属于注入的一种,相信大家在之前的课程中都知道POST\GET两种传参方式. POST注入就是使用POST进行传参的注入,本质上和GET类型的没什么区别. POST注入高危 ...
- CentOS7 忘记Root密码解决方法
1- 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在 ...
- Python踩坑系列之使用redis报错:module 'redis' has no attribute 'Redis'问题
初次使用redis时,在链接Redis后,运行报错“module 'redis' has no attribute 'Redis' ”. 具体代码如下: import redis r = redis. ...
- JPA的一些问题
Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'test ...
- MVC、MTV、FBV、CBV、母版和继承:
cookie session cookie的定义: 保存在浏览器上的一组组键值对 (请求头) 为什么要有? http协议是无状态,每次的请求之间是相互独立的,没有办法保存状态. Django中操作co ...
- Java四个关键字 this super final static
一.this 关键字主要有三个应用: this调用本类中的属性,也就是类中的成员变量: this调用本类中的其他方法: this调用本类中的其他构造方法初始化对象,调用时要放在构造方法的首行. 引 ...
- WPF 精修篇 DataGrid 筛选
原文:WPF 精修篇 DataGrid 筛选 DataGrid也可以分组 但是用的地方不多 就没写 筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选 贴码 <DataGrid x: ...