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叔叔耍了~~

  • 相关阅读:
    TextView 高亮
    Android 学习 第一章(环境搭建)
    从assets res 中读文件
    动态设置imageview 宽高
    android 算定义view 打包 jar(一次开发多次使用)
    Activity 跳转
    Android手机在开发调试时logcat不显示输出信息的解决办法
    弹出对话 AlertDialog 有按钮
    让划动 listview时 没有黑色背景
    Activity Service 数据相互操作
  • 原文地址:https://www.cnblogs.com/teddyma/p/261546.html
Copyright © 2011-2022 走看看