.
.
.
// elsewhere at app startup time attach the handler to the AppDomain.AssemblyResolve event
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
.
.
.
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName assemblyName = new AssemblyName(args.Name);
// this.ReadOnlyPaths is a List<string> of paths to search.
foreach (string path in this.ReadOnlyPaths)
{
// If specified assembly is located in the path, use it.
DirectoryInfo directoryInfo = new DirectoryInfo(path);
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
{
string fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");
if (assemblyName.Name.ToUpperInvariant() == fileNameWithoutExt.ToUpperInvariant())
{
return Assembly.Load(AssemblyName.GetAssemblyName(fileInfo.FullName));
}
}
}
return null;
}
3条答案
按热度按时间kzipqqlq1#
您可以处理AppDomain.AssemblyResolve事件,并使用Assembly.LoadFile自行从目录加载程序集,该文件提供了程序集尝试解析的程序集的完整路径。
示例:
nimxete22#
您可以在app.config中的assemblyBinding〉probing::privatePath标记中设置文件夹路径,以便公共语言运行库在加载程序集时进行搜索。
Reference MSDN
9rbhqvlz3#
您可以创建类:
由于WPF已经有了一个内置的
Main()
方法,你会得到一个编译器错误。所以转到项目属性〉应用程序〉“启动对象”并将其设置为myNamespace.EntryPoint
。在你自己的Main()
方法中,你可以完全控制一切,所以你可以在示例化App
之前设置AssemblyResolve
处理程序。