还是来看看出错的典型场景:
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叔叔耍了~~