Loading Assemblies from a .Net Application in a 'Sandbox Environment'

前端 未结 2 1576
盖世英雄少女心
盖世英雄少女心 2021-01-01 07:16

I am currently developing an application in where a user will dynamically choose dlls and the application will execute some of the methods in that dll. (if you follow t

相关标签:
2条回答
  • 2021-01-01 07:41

    What you're looking to do is basically running Assemblies in a separate Application Domain. Check out this page on MSDN for a good starting point. It's actually quite east to do:

    http://msdn.microsoft.com/en-us/library/ms173139(VS.80).aspx

    0 讨论(0)
  • 2021-01-01 07:54

    Normally, you might just live with

    AppDomain newDomain = AppDomain.CreateDomain(name);
    Assembly asm = newDomain.Load(System.IO.File.ReadAllBytes(name));
    

    But one interesting point is, the AppDomain.Load method will load the assembly to the new app domain, as well as to the current domain.

    A more elegant solution is to use System.AddIn namespace in 3.5. - http://msdn.microsoft.com/en-us/magazine/cc163476.aspx

    Then, you can actually specify the trust level for your addin, using the AddinSecurityLevel like

    //Activate the selected AddInToken in a new
    //application domain with the Internet trust level.
    Calculator CalcAddIn = selectedToken.Activate<Calculator>(AddInSecurityLevel.Internet);
    

    See http://msdn.microsoft.com/en-us/library/bb355219.aspx for details.

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