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

Suppose you are provided with the assembly Class.dll, composed of the following simple code:

namespace ClassLibrary
{
    public class Class
    {
    }
}

And consider another project with the above Class.dll as a reference to the project 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 statement fails, and I don't understand why.

The statement succeeds if I replace ClassLibrary.Class with, say, System.Text.RegularExpressions.Regex and Class.dll with System.dll, so I assume this has something to do with the project properties? maybe some compiler?

Thank you in advance

+3
source share
3 answers

: , .LoadFrom, "", Fusion (.Load). CLR. CLR.

+8

, .

knownType.Assembly reflectedType.Assembly .

+2

I would suggest that you are not referencing the same assembly that you are loading from disk.

This example (when compiled as Test.exe) works just fine:

using System;
using System.Reflection;

class Example
{
    static void Main()
    {
        Assembly assembly = Assembly.LoadFrom(@"Test.exe");

        Type reflectedType = assembly.GetType("Example");
        Type knownType = typeof(Example);

        Console.WriteLine(reflectedType == knownType);
    }
}
+2
source

Source: https://habr.com/ru/post/1719456/


All Articles