如何加载一个.exe作为.NET程序集?加载、程序、exe、NET

2023-09-03 10:19:50 作者:随大众i

我可以只使用:

Assembly.LoadFile

不知道这是做这样的方式?

Not sure if this is the way to do this?

但是当我尝试这种方法,它抛出一个无法加载文件或程序CustomControlLib或它的一个依赖。该系统找不到指定的文件。

But when I try that approach, it throws a Could not load file or assembly "CustomControlLib" or one of its dependencies. The system cannot find the file specified.

任何想法?

推荐答案

您需要确保的依赖也被加载到应用程序域。如果他们没有自动定位,您可以订阅 AppDomain.AssemblyResolve 以手工查找和加载组件如果需要的话。

You will need to make sure that the dependencies are also loaded into the app domain. If they aren't located automatically, you can subscribe to AppDomain.AssemblyResolve in order to find and load assemblies manually if needs be.

例如:

private Assembly AssemblyEventHandler(object sender, ResolveEventArgs args)
{
    return locatedAssembly;
}

另外,我会考虑使用 Assembly.LoadFrom ,特别是阅读本具有由nobugz和链接的一些良好的读取的(所有过时,但应该仍然可以承受的大部分。)

Also, I would consider using Assembly.LoadFrom, particularly after reading this which has a strong assertion by nobugz and links to some good reading (all dated but ought to still be withstanding for the most part.)