Assembly.GetType()和typeof运算()返回不同类型的? [英] Assembly.GetType() and typeof() return different types?
问题描述
假设你给出从以下简单的代码编译一个Class.dll组件:
Suppose you are given a Class.dll assembly compiled from the following simple code:
namespace ClassLibrary
{
public class Class
{
}
}
并考虑上述Class.dll不同的项目作为项目参考和使用下面的代码:
And consider a different project with the above Class.dll as a project reference and with the following code:
Assembly assembly = Assembly.LoadFrom(@"Class.dll");
Type reflectedType = assembly.GetType("ClassLibrary.Class");
Type knownType = typeof(ClassLibrary.Class);
Debug.Assert(reflectedType == knownType);
断言失败了,我不明白为什么。
The assertion fails, and I don't understand why.
断言成功,如果我取代ClassLibrary.Class用,也就是说,System.Text.RegularExpressions.Regex类Class.dll与System.dll中,所以我猜它有什么做的项目属性?一些编译开关吧?
The assertion succeeds if I replace the ClassLibrary.Class with, say, the System.Text.RegularExpressions.Regex class and Class.dll with System.dll, so I'm guessing it has something to do with the project properties ? some Compilation switch perhaps?
在此先感谢
推荐答案
问题是负载环境:通过.LoadFrom加载的程序集都保存在一个不同的一堆,比那些融合(.Load)加载。的类型是到CLR实际上是不同的。检查此链接从一个CLR建筑师更多的细节。
The problem is load contexts: assemblies loaded via .LoadFrom are kept in a different "pile" than those loaded by Fusion (.Load). The types are actually different to the CLR. Check this link for more detail from a CLR architect.
这篇关于Assembly.GetType()和typeof运算()返回不同类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!