Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.GetType() and typeof() return different types?

Suppose you are given a Class.dll assembly compiled from the following simple code:

namespace ClassLibrary
{
    public class Class
    {
    }
}

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.

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?

Thanks in advance

like image 295
Ohad Schneider Avatar asked Dec 04 '25 00:12

Ohad Schneider


1 Answers

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.

like image 173
nitzmahone Avatar answered Dec 05 '25 14:12

nitzmahone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!