Types loaded from assembly file are not equal to the type mentioned in .NET Core

I take Type , request its assembly location, and then load the assembly from the same address and find the same type from the loaded assembly. The resulting type is not equal to the original type.

Here's a test case:

 [TestMethod] public void TestTypeLoadingWithFilePath() { var originalType = typeof(SomeClass); var assemblyAddress = originalType.Assembly.Location; var loadedAssembly = Assembly.LoadFile(assemblyAddress); Assert.IsNotNull(loadedAssembly); var loadedType = loadedAssembly.GetType(originalType.FullName); Assert.IsNotNull(loadedType); Assert.AreEqual(originalType, loadedType); } 

The test fails at the last statement.

This only happens on .NET Core on Windows. (I am testing the latest version, 2.1.4). But this was not the case with the .NET Framework.

My questions:

  • Is it design or bug?
  • If it is by design, why?
  • Again, if it's by design, doesn't that mean different behavior between the two .NET Standard implementations? (.NET Core and .NET Framework)
+7
c # .net-core
source share
1 answer

This is normal behavior. Using Assembly.LoadFile load the assembly and create a new "instance". To fix this, simply use Assembly.LoadFrom . At first it will look in the current context if the requested assembly is already loaded, and take it if it is. Then a comparison of types like the ones you do will work.

Edit: I don't know if it is intended, but this method works in both .NetFramework and .NetCore.

+4
source share

All Articles