Need to hookup AssemblyResolve event when DisallowApplicationBaseProbing = true

妖精的绣舞 提交于 2019-11-27 15:19:54

With lots of experiments I got the following working:

internal class AssemblyResolver : MarshalByRefObject
{
  static internal void Register(AppDomain domain)
  {
    AssemblyResolver resolver =
        domain.CreateInstanceFromAndUnwrap(
          Assembly.GetExecutingAssembly().Location,
          typeof(AssemblyResolver).FullName) as AssemblyResolver;

    resolver.RegisterDomain(domain);
  }

  private void RegisterDomain(AppDomain domain)
  {
    domain.AssemblyResolve += ResolveAssembly;
    domain.AssemblyLoad += LoadAssembly;
  }

  private Assembly ResolveAssembly(object sender, ResolveEventArgs args)
  {
    // implement assembly resolving here
    return null;
  }

  private void LoadAssembly(object sender, AssemblyLoadEventArgs args)
  {
    // implement assembly loading here
  }
}

The domain is created like this:

    AppDomainSetup setupInfo = AppDomain.CurrentDomain.SetupInformation;
    setupInfo.DisallowApplicationBaseProbing = true;

    domain = AppDomain.CreateDomain("Domain name. ", null, setupInfo);
    AssemblyResolver.Register(domain);

Sorry, I can't share the code for resolving and loading the assemblies. Firstly, it doesn't work yet. Secondly, it will be too much and too specific to share with the general public in this context.

I'll be bootstrapping an object structure that is serialized alongside the assemblies required for deserialization from a single file. For this, I really deserve going straight to .dll hell.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!