How to load an .exe as a .NET assembly?

帅比萌擦擦* 提交于 2019-12-20 10:16:44

问题


Can I just use?:

Assembly.LoadFile

Not sure if this is the way to do this?

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.

Any ideas?


回答1:


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.

For example:

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

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.)




回答2:


Yes, you can load a .Net exe the same way as you would load a Dll. The error you get is caused by dependencies of your exe. Make sure that the those dependencies can be found, i.e. are in your assembly search path.




回答3:


If you use Assembly.LoadFrom() dependencies will be loaded from the directory where assembly is located.

Assembly.LoadFile() is ignoring all other DLLs and exe files in the same folder.




回答4:


Yes, you can use Assembly.LoadFile(). Check your path.



来源:https://stackoverflow.com/questions/5797205/how-to-load-an-exe-as-a-net-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!