• 让人哭笑不得的“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叔叔耍了~~

  • 相关阅读:
    Codeforces Round448 D
    Codeforces Round448 C Square Subsets
    hdu6006
    2017计蒜客计算之道初赛第六场 微软大楼设计方案(困难)
    UVA 12105 Bigger is Better
    Codeforce 55D Beautiful numbers
    4月补题
    C++小技巧之CONTAINING_RECORD
    Codeforces #380 div2 E(729E) Subordinates
    Codeforces #380 div2 D(729D) Sea Battle
  • 原文地址:https://www.cnblogs.com/teddyma/p/261546.html
Copyright © 2020-2023  润新知