zoukankan      html  css  js  c++  java
  • 让人哭笑不得的“Unable to load one or more of the types in the assembly”问题的解决!

    这两天一直在为“Unable to load one or more of the types in the assembly”而头疼,我相信凡是有需要大量使用Reflection来Load Assembly,并动态地调用ass.GetTypes()或ass.GetType(string)或CreateInstance()的朋友可能会和我一样遇到这个恼人的问题。而且这个问题是非常隐蔽的,一般如果GetType或CreateInstance执行时遇到这个问题,而选择出错不提示的话,程序只是简单的返回null。而且,至今未找到任何描述为何会这样原因的文档。

    还是来看看出错的典型场景:

    Assembly ass = Assembly.LoadFile(assPath);
    t = ass.GetType(typeName, false);

    就是这么明显的代码,却时常会发生这个“Unable to load one or more of the types in the assembly”错误,明明指定的Type在这个Assembly中,却报错并返回null。而且,有的程序集运行正常,有的则不行,甚至,有的有时正常有时报错,简直是对Bill没话说。

    几经周折,发现解决办法:用LoadFrom代替LoadFile:

    Assembly ass = Assembly.LoadFrom(assPath);
    t = ass.GetType(typeName, false);


    的确很简单,简单到我想笑,但是,在网上找了无数圈,无数的朋友却都没尝试这样做而烦恼不已。

    比较MSDN中Assembly.LoadFile和Assembly.LoadFrom的描述:

    Assembly.LoadFile:
    Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

    Loads the contents of an assembly file.


    Assembly.LoadFrom:
    Loads an assembly.

    除此之外没有任何不同的说明!

    无话说,只能感叹又一次被Bill叔叔耍了~~

  • 相关阅读:
    sinaapp+wordpress
    用JavaScript实现本地缓存
    Javascript的IE和Firefox(火狐)兼容性
    150个javascript特效
    学习Javascript闭包(Closure)
    JS bingo卡片
    Response.Write详细介绍[转]
    JS实现鼠标经过时背景色缓慢改变
    为什么引用不了App_Code里的类
    SQL SERVER 2008 R2配置管理器出现“远程过程调用失败”【0x800706be】的解决办法
  • 原文地址:https://www.cnblogs.com/teddyma/p/261546.html
Copyright © 2011-2022 走看看