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

来源:互联网 发布:软件项目验收管理制度 编辑:程序博客网 时间:2024/06/15 06:09

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