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

0
0
(请您对文章做出评价)
« 上一篇:AOP Practice with AspectWeaver0.6 - DebugPropertyValueModifying
» 下一篇:AJAX设计模式 之 怎样构建一个可刷新的无刷新应用
posted @ 2005-10-25 14:02 Teddy's Knowledge Base 阅读(2307) 评论(5)  编辑 收藏 网摘

  回复  引用    
#1楼2005-10-25 15:05 | microhf[未注册用户]
楼主我体谅你的心情,下面这是nhibernate中的ReflectHelper的一段源码。
/// <summary>
/// Returns a <see cref="System.Type"/> from an already loaded Assembly or an
/// Assembly that is loaded with a partial name.
/// </summary>
/// <param name="className">The full name of the class.</param>
/// <param name="assemblyName">
/// The name of the assembly. This can be the full assembly name or just the partial name.
/// </param>
/// <returns>
/// The <see cref="System.Type"/> for the class in the assembly or
/// <c>null</c> if a <see cref="System.Type"/> can't be found.
/// </returns>
/// <remarks>
/// Attempts to get a reference to the type from an already loaded assembly. If the
/// Type can be found then the Assembly is loaded using LoadWithPartialName.
/// </remarks>
public static System.Type TypeFromAssembly( string className, string assemblyName )
{
try
{
// try to get the Types from an already loaded assembly
System.Type type = System.Type.GetType( className + ", " + assemblyName );

// if the type is null then the assembly is not loaded.
if( type==null )
{
// use the partial name because we don't know the public key, version, culture-info of
// the assembly on the local machine.
Assembly assembly = Assembly.LoadWithPartialName( assemblyName );
if( assembly!=null )
{
type = assembly.GetType( className );
}
}

return type;
}
catch( Exception e )
{
if( log.IsErrorEnabled )
{
log.Error( className + ", " + assemblyName + " could not be loaded.", e );
}
return null;
}
}

btw:why not open your source?

  回复  引用    
#2楼2005-10-25 15:09 | microhf[未注册用户]
我想上面的可以得到一个System.Type
那么Activator.CreateInstance(System.Type)就可以得到你要得对象实例。

  回复  引用    
#3楼2005-10-26 10:15 | ly4cn
就觉得奇怪,我怎么没遇到这个问题。我一直都是用LoadFrom,从来没用过LoadFile
  回复  引用  查看    
#4楼2005-10-26 14:12 | Kozen      
Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. Do not use LoadFile to load assemblies that you want to execute.
LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.

  回复  引用    
#5楼2007-05-28 17:58 | 微微[未注册用户]
郁闷,去年遇到了这个问题,绕道绕过去了!
今天没有在代码中用到 Assembly ,还有这个问题!!!