当用到反射的程序部署时出现System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more
来源:互联网 发布:鲁班节点软件 编辑:程序博客网 时间:2024/05/09 10:47
出现这个错误是因为某些dll无法用LoadFrom加载造成的,一般情况下是环境中缺少这个dll。另外一种情况,当你是遍历bin来反射dll时,如果不影响业务,可以把那些加载不上的dll忽略掉(前提是忽略掉的dll不影响业务)。
下面的try catch的位置,一旦有一个dll加载不上,就直接抛出异常,程序不再往下进行
public static string path = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\";
public Dictionary<string,string> GetActivityContainer()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string tempName = "";
try
{
string[] dicFileName = Directory.GetFileSystemEntries(path);
if (dicFileName != null)
{
List<string> assemblyList = new List<string>();
foreach (string name in dicFileName)
{
if (name.ToLower().IndexOf(".dll") == -1)
{
continue;
}
Assembly assembly = Assembly.LoadFrom(name);
Type[] ts = assembly.GetTypes();
List<string> classList = new List<string>();
foreach (Type t in ts)
{
if (t.GetInterfaces().Length == 0)
{
continue;
}
if ( t.GetInterfaces()[0] != typeof(IActivity))
{
continue;
}
var attrArr = t.GetCustomAttributes(typeof(ActiveAttribute), true);
if (attrArr.Length == 0)
{
continue;
}
ActiveAttribute attr = attrArr[0] as ActiveAttribute;
if (attr != null)
{
tempName = name.Substring(name.LastIndexOf("\\") + 1);
dic.Add(attr.Msg, tempName);
}
}
}
}
}
catch (Exception e)
{
Log4NetHelp.Log.Error(e.Message);
throw new Exception(e.Message);
}
return dic;
}
public static string path = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\";
public Dictionary<string,string> GetActivityContainer()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string tempName = "";
try
{
string[] dicFileName = Directory.GetFileSystemEntries(path);
if (dicFileName != null)
{
List<string> assemblyList = new List<string>();
foreach (string name in dicFileName)
{
if (name.ToLower().IndexOf(".dll") == -1)
{
continue;
}
Assembly assembly = Assembly.LoadFrom(name);
Type[] ts = assembly.GetTypes();
List<string> classList = new List<string>();
foreach (Type t in ts)
{
if (t.GetInterfaces().Length == 0)
{
continue;
}
if ( t.GetInterfaces()[0] != typeof(IActivity))
{
continue;
}
var attrArr = t.GetCustomAttributes(typeof(ActiveAttribute), true);
if (attrArr.Length == 0)
{
continue;
}
ActiveAttribute attr = attrArr[0] as ActiveAttribute;
if (attr != null)
{
tempName = name.Substring(name.LastIndexOf("\\") + 1);
dic.Add(attr.Msg, tempName);
}
}
}
}
}
catch (Exception e)
{
Log4NetHelp.Log.Error(e.Message);
throw new Exception(e.Message);
}
return dic;
}
下面的try catch的位置则可以让程序忽略掉加载不上的dll继续执行
Dictionary<string, string> dic = new Dictionary<string, string>();
string tempName = "";
string[] dicFileName = Directory.GetFileSystemEntries(path);
if (dicFileName != null)
{
List<string> assemblyList = new List<string>();
foreach (string name in dicFileName)
{
try
{
if (name.ToLower().IndexOf(".dll") == -1)
{
continue;
}
Assembly assembly = Assembly.LoadFrom(name);
Type[] ts = assembly.GetTypes();
List<string> classList = new List<string>();
foreach (Type t in ts)
{
try
{
if (t.GetInterfaces().Length == 0)
{
continue;
}
if (t.GetInterfaces()[0] != typeof(IActivity))
{
continue;
}
var attrArr = t.GetCustomAttributes(typeof(ActiveAttribute), true);
if (attrArr.Length == 0)
{
continue;
}
ActiveAttribute attr = attrArr[0] as ActiveAttribute;
if (attr != null)
{
tempName = name.Substring(name.LastIndexOf("\\") + 1);
dic.Add(attr.Msg, tempName);
}
}
catch (Exception ex)
{
Log4NetHelp.Log.Error(ex.Message);
}
}
}
catch (Exception e)
{
Log4NetHelp.Log.Error(e.Message);
}
}
}
return dic;
}
- 当用到反射的程序部署时出现System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more
- System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the r
- 让人哭笑不得的“Unable to load one or more of the types in the assembly”问题的解决!
- Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies
- Could not load file or assembly system.data.sqlite.dll or one for it's depedencies
- Fix: 'Unable to load one or more of the requested types' Errors
- C# 反射:System.Reflection
- 反射 System.Reflection
- Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made
- SQLite 解决:Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
- SQLite 解决:Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
- System.Reflection 反射技术实例.
- C#反射 总结 (System.Reflection )
- C# System.Reflection (反射)
- C# System.Reflection (反射)
- Unity dll Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the mod
- 坑_Unity中导入自建DLL报错:Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in
- System.DllNotFoundException Unable to load DLL SQLite.Interop.dll解决办法
- JQuery技巧|教你如何更快的赋值取值
- Arrays.asList方法的漏洞
- 最新最经典的黑客入门教程(必备技能)
- php中的基类,派生类
- 更新用blob字段存储照片的Java代码
- 当用到反射的程序部署时出现System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more
- CC3D的资源
- Oracle Create Sequence
- java静态代理和动态代理
- WIX心得
- Spring Data Repository有趣的定义query方法
- tomcat启动服务闪退的问题
- 项目开发和管理需要弄清楚的6个问题
- 设计模式学习(UML)