mardi 27 octobre 2020

Get the full name of a referenced type without loading its assembly

I have a .Net project where I'm given an assembly (*.dll), and I have to list out the contained types and their members. However, I'm not given the assembly's references.

Suppose I'm given A.dll which has a type in it:

public class TypeInA : IInterfaceInB
{
    ...
}

Since I'm not given B, I get a FileNotFoundException when I try to call

typeof(TypeInA).GetInterfaces()

because it can't find B.dll.

I don't need the details about IInterfaceInB, just its namespace-qualified name. Is there a way I can get this without having to load B.dll?

More context

I'm following the MetadataLoadContext docs to load A.dll and enumerate its types:

var runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");
var paths = new List<string>(runtimeAssemblies) {path};
var resolver = new PathAssemblyResolver(paths);
using var context = new MetadataLoadContext(resolver);

var assembly = context.LoadFromAssemblyPath(path);




Aucun commentaire:

Enregistrer un commentaire