Need to hookup AssemblyResolve event when DisallowApplicationBaseProbing = true

前端 未结 1 1435
Happy的楠姐
Happy的楠姐 2020-12-03 20:01

I need to hookup an AssemblyResolve event on my created AppDomain when I\'ve set DisallowApplicationBaseProbing = true. The reason I\'m doing this is to force the runtime t

相关标签:
1条回答
  • 2020-12-03 20:20

    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.

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