7

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
5
  • Are you using Assembly.ReflectionOnlyLoad()? Commented Oct 2, 2017 at 11:42
  • @MartinUllrich I have tried using that either. But the point is that it fails to load System.Runtime.dll Commented Oct 2, 2017 at 12:21
  • 3
    .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. Commented Oct 2, 2017 at 13:18
  • @HansPassant but how do tools like Reflector and dnspy manage to browse assemblies irrespective of their target framework? Commented Oct 2, 2017 at 13:29
  • 2
    Reflector does not use reflection, it uses the low-level metadata interfaces. IMetaDataAssemblyImport and friends. The metadata format is the same. Commented Oct 2, 2017 at 13:31

1 Answer 1

5

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...
Sign up to request clarification or add additional context in comments.

1 Comment

These are actually two answers. Although the comment by Hans Passant is highlighted, I would like to point out that the reference to Mono.Cecil helped me in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.