Tôi đang thử nghiệm với việc tải một assembly bằng cách sử dụng các mảng byte, nhưng tôi không thể tìm ra cách để nó hoạt động đúng cách. Đây là thiết lập:Đang tải Assembly Byte Array
public static void Main()
{
PermissionSet permissions = new PermissionSet(PermissionState.None);
AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory };
AppDomain friendlyDomain = AppDomain.CreateDomain("Friendly", null, setup, permissions);
Byte[] primary = File.ReadAllBytes("Primary.dll_");
Byte[] dependency = File.ReadAllBytes("Dependency.dll_");
// Crashes here saying it can't find the file.
friendlyDomain.Load(dependency);
AppDomain.Unload(friendlyDomain);
Console.WriteLine("Stand successful");
Console.ReadLine();
}
Tôi đã tạo hai dlls giả và đổi tên thành tiện ích mở rộng thành '.dll_' để hệ thống không thể tìm thấy tệp thực. Cả hai primary
và dependency
điền chính xác, nhưng khi tôi cố gắng gọi phương thức AppDomain.Load
với dữ liệu nhị phân, nó trở lại với:
Could not load file or assembly 'Dependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Tại sao nó sẽ được tìm kiếm hệ thống cho một tập tin?
CẬP NHẬT
này mặt khác dường như làm việc:
public class Program {
public static void Main() {
PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory };
AppDomain friendlyDomain = AppDomain.CreateDomain("Friendly", null, setup, permissions);
Byte[] primary = File.ReadAllBytes("Primary.dll_");
Byte[] dependency = File.ReadAllBytes("Dependency.dll_");
// Crashes here saying it can't find the file.
// friendlyDomain.Load(primary);
Stage stage = (Stage)friendlyDomain.CreateInstanceAndUnwrap(typeof(Stage).Assembly.FullName, typeof(Stage).FullName);
stage.LoadAssembly(dependency);
Console.WriteLine("Stand successful");
Console.ReadLine();
}
}
public class Stage : MarshalByRefObject {
public void LoadAssembly(Byte[] data) {
Assembly.Load(data);
}
}
Vì vậy, nó dường như có sự khác biệt giữa AppDomain.Load
và Assembly.Load
.
Liệu DLL phụ thuộc có bất kỳ phụ thuộc nào chưa được sao chép, có lẽ không? –
Tiểu học phụ thuộc vào sự phụ thuộc. Mặc dù phụ thuộc không có phụ thuộc (non-CLR). Dường như thời gian chạy không nên tìm kiếm tệp để bắt đầu. – sircodesalot