1

Let's say, we have class Base, and Derived, defined in Base.dll and Derived.dll respectively. Derived is derived from Base (may not be a direct sub-class though)

The problem at hand is, if we have Derived.dll at hand but Base.dll is missing, how to programmatically examine all the types in Derived? e.g. to know what types are available, their accessibility, inheritance relationship, etc

Based on my understanding, reflection (things in System.Reflection namespace) is not an option here because GetTypes() will try to load Base.dll which is not available, thus throw ReflectionTypeLoadException.

In particularly, is this something that can be easily achieved using Roslyn, or some good library?

ohw
  • 1,560
  • 11
  • 20
  • I guess that it is not possible. .NET assemblies don't directly implement the types of base classes in order to support Polymorphism. – Ian H. Nov 17 '16 at 08:14
  • 2
    You'd probably have to use Mono.Cecil for this. Roslyn is the compiler not a metadata reader (but obvs it has to read metadata to do compilation). You won't be able to work out all relationships though? What if there is an interface implemented by a base class. You don't have that type info because the assembly is not available. – Mike Zboray Nov 17 '16 at 08:15
  • Perhaps this thread may help you: http://stackoverflow.com/questions/2646707/something-better-than-net-reflector – Peter B Nov 17 '16 at 08:33
  • Have you tried [Assembly.ReflectionOnlyLoad](https://msdn.microsoft.com/en-us/library/0et80c7k(v=vs.110).aspx)? – elchido Nov 18 '16 at 15:07
  • @elchido Once I call ``GetTypes()`` on an assembly loaded with ``ReflectionOnlyLoad``, it will still try to load all its dependencies, so ReflectionOnlyLoad is not an option here. – ohw Dec 08 '16 at 02:43

2 Answers2

3

You're looking for System.Reflection.Metadata, which exposes assembly metadata directly, without loading assemblies via reflection.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
1

For investigate .NET assembly (types, methods etc.) you need to get that from the metadata. Roslyn its not what you are looking for.

Although it's not exactly true, because Roslyn has two types of information about your code, one is nodes and token and the other is symbols. The first don't know about relation info but the second know.

Anyway, you can do it in more than one way. I'll write two of them.

  1. Use Mono.Cecil to open your assembly, get the main module and investigate whatever you want.

    var allTypesDefinitaion = ModuleDefinition.ReadModule(assemblyPath).Types;
    
  2. Use tools like CFF Explorer to investigate the relevant metadata tables. (TypeDef\Ref)

binki
  • 6,057
  • 3
  • 49
  • 84
Dudi Keleti
  • 2,686
  • 16
  • 30