C# MEF DirectoryCatalog's unexpected result with identical DLL file name

元气小坏坏 提交于 2020-05-15 07:52:09

问题


I'm developing some WPF application which supports MEF plug-in features. It founds plugin by get exports from DirectoryCatalog, like

DirectoryCatalog catalog = new DirectoryCatalog(@".\libs", fileName);
CompositionContainer container = new CompositionContainer(catalog);
BaseModel.Instance.ImportedPlugins = container.GetExports<IExternalServiceProvider>();

So basically, I wanted this to load FILENAME.dll inside of the sub folder which named libs. However, when the dll file named FILENAME.dll exists both main folder and libs folder, that code loads FILENAME.dll of main folder.

Debug
ㄴlibs
  ㄴFILENAME.dll(latest build) - This one should be loaded
ㄴApp.exe
ㄴFILENAME.dll(old build) - but this one is loaded instead

I tried several scenarios, but it makes me more confusing.

CASE 1

Debug
ㄴlibs
  ㄴFILENAME.dll(latest version)
ㄴApp.exe
ㄴFILENAME.dll(old version)

-> old version is loaded

CASE 2

Debug
ㄴlibs
  ㄴFILENAME.dll(latest version)
ㄴApp.exe
ㄴFILENAME.dlllll(old version)

-> latest version is loaded

CASE 3

Debug
ㄴlibs
  ㄴFILENAME.dll(latest version)
ㄴApp.exe
ㄴWRONGFILENAME.dll(old version)

-> latest version is loaded

CASE 4 (Most confusing case)

Debug
ㄴlibs
  ㄴWRONGFILENAME.dll or FILENAME.dlllll(latest version)
ㄴApp.exe
ㄴFILENAME.dll(old version)

-> failed to load any of them

I may try different way of using Catalog to solve this loading problem, but I can't find why and how Case 4 happens. What was I doing wrong?

来源:https://stackoverflow.com/questions/61587096/c-sharp-mef-directorycatalogs-unexpected-result-with-identical-dll-file-name

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