现在的位置: 首页 > 综合 > 正文

How a AppDomain Can load an assembly by it’s physical path.

2019年10月07日 ⁄ 综合 ⁄ 共 961字 ⁄ 字号 评论关闭

AppDomain Only have the Load() method which can only load assembly by name. What if you want to load from the physical path? See the code following:

// Assembly you want to load
String assemblyFileName = @"D:TestTest.exe";
String assemblyName = @"Test";

// Setup the evidence
Evidence evidence = new Evidence( AppDomain.CurrentDomain.Evidence );

AppDomain TestDomain = AppDomain.CreateDomain(
  // The friendly name of the domain.
  "TestDomain",                  
  // Evidence mapped through the security policy to establish a top-of-stack permission set.
  evidence,                          
  // The base directory that the assembly resolver uses to probe for assemblies.
  AppDomain.CurrentDomain.BaseDirectory,       
  // The path relative to the base directory where
  // the assembly resolver should probe for private assemblies.
  System.IO.Path.GetFullPath( assemblyFileName ),        
  // If true, a shadow copy of an assembly is loaded into this application domain.
  true );
 
Console.WriteLine( TestDomain.Load( assemblyName ).FullName );
AppDomain.Unload( TestDomain );  // Unload the AppDomain

抱歉!评论已关闭.