Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a .NET Core assembly from an application written in .NET Framework?

We have an application written in .NET which browses .NET assemblies using Reflection API. When we use this application to browse dll compiled with .NET Core 2.0, it fails with following exception. Any help regarding this will be appreciated.

Unhandled Exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at ConsoleApp1.Program.NewMethod() in c:\Users\faisal_iqbal\documents\visual studio 2017\Projects\CoreX\ConsoleApp1\Program.cs:line 23
   at ConsoleApp1.Program.Main(String[] args) in c:\Users\faisal_iqbal\documents\visual studio 2017\Projects\CoreX\ConsoleApp1\Program.cs:line 16
like image 253
Faisal Avatar asked Oct 02 '17 09:10

Faisal


1 Answers

Answering my own question.

NETCore is a very different framework. If you build that assembly by targeting .NETStandard 2.0 instead and target your app to at least 4.6.1 then you can do this.

– Hans Passant

Since both .NET Framework and .NET Core are different frameworks, each has its own Reflection API.

If you need to load assemblies merely for meta data reading purpose, use Mono.Cecil framework which works for both .NET Framework as well as for .NET Core. It doesn't load the assembly or dependent assemblies as Reflection does. It simply reads the meta data information like

  • Types in assembly
  • Properties, fields and methods in a type
  • Name, FullName and Namespace of a type
  • etc...
like image 52
Faisal Avatar answered Oct 19 '22 11:10

Faisal