C# 查找对已加载DLL的引用
我有一个可以通过编写自定义扩展来定制的应用程序。它们都在C# 查找对已加载DLL的引用,c#,dll,mef,C#,Dll,Mef,我有一个可以通过编写自定义扩展来定制的应用程序。它们都在proj\Extensions文件夹中。在运行时,myCoreproject从文件夹中加载每个扩展并执行代码。问题是其中一个扩展使用其他库时,因为Core项目找不到这些附加库的引用 例如,在我的Core项目中,我有: public void Preview(IFileDescription fileDescription) { var extension = Path.GetExtension(fileDescription.Fil
proj\Extensions
文件夹中。在运行时,myCore
project从文件夹中加载每个扩展并执行代码。问题是其中一个扩展使用其他库时,因为Core
项目找不到这些附加库的引用
例如,在我的Core
项目中,我有:
public void Preview(IFileDescription fileDescription)
{
var extension = Path.GetExtension(fileDescription.FilePath);
var reader = _readerFactory.Get(extension);
Data = reader.GetPreview(fileDescription);
}
在我的一个扩展中
public DataTable GetPreview(IFileDescription options)
{
var data = new DataTable();
using (var stream = new StreamReader(options.FilePath))
{
var reader = new CsvReader(stream); // <- This is from external library and because of this Core throws IO exception
}
/*
...
*/
return data;
}
公共数据表GetPreview(IFileDescription选项)
{
var data=new DataTable();
使用(var stream=newstreamreader(options.FilePath))
{
var reader=new CsvReader(stream);//是的,这是可能的。您可以附加到事件并从外接程序目录手动加载所需的DLL。在执行任何外接程序代码之前,请执行以下代码:
var addinFolder = ...;
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
var missing = new AssemblyName(e.Name);
var missingPath = Path.Combine(addinFolder, missing.Name + ".dll");
// If we find the DLL in the add-in folder, load and return it.
if (File.Exists(missingPath))
return Assembly.LoadFrom(missingPath);
// nothing found, let .NET search the common folders
return null;
};
太好了!谢谢。我会在7分钟内接受这个答案:)