How Do I Load an Assembly and All of its Dependencies at Runtime in C# for Reflection?

后端 未结 2 1151
情书的邮戳
情书的邮戳 2021-02-08 00:00

I\'m writing a utility for myself, partly as an exercise in learning C# Reflection and partly because I actually want the resulting tool for my own use.

What I\'m after

相关标签:
2条回答
  • 2021-02-08 00:06

    I worked out Kent Boogaart's second option. Essentially I had to:

    1.) Implement the ResolveEventHandler in a separate class, inheriting from MarshalByRefObject and adding the Serializable attribute.

    2.) Add the current ApplicationBase, essentially where the event handler's dll is, to the AppDomain PrivateBinPath.

    You can find the code on github.

    0 讨论(0)
  • 2021-02-08 00:18

    Couple of options here:

    1. Attach to AppDomain.AssemblyResolve and do another LoadFile based on the requested assembly.
    2. Spin up another AppDomain with the directory as its base and load the assemblies in that AppDomain.

    I'd highly recommend pursuing option 2, since that will likely be cleaner and allow you to unload all those assemblies after. Also, consider loading assemblies in the reflection-only context if you only need to reflect over them (see Assembly.ReflectionOnlyLoad).

    0 讨论(0)
提交回复
热议问题