2014-01-13 29 views
0

我們正在使用Catel.MVVM開發GUI插件框架。 應使用「ServiceLocatorRegistration」屬性動態加載單個插件。Catel自動註冊多個實例的屬性

例子:

[ServiceLocatorRegistration(typeof(IInitializable), ServiceLocatorRegistrationMode.SingletonInstantiateWhenRequired, "SamplePlugin")]

在我們的引導程序,我們所有的插件組件加載到默認的AppDomain:通過調用

var initializables = ServiceLocator.Default.ResolveTypes(); 
foreach(var initializable in initializables) 
    initializable.Initialize(); 

但即使

 Catel.Windows.Controls.MVVMProviders.Logic.UserControlLogic.DefaultSkipSearchingForInfoBarMessageControlValue = true; 
     Catel.Windows.Controls.MVVMProviders.Logic.UserControlLogic.DefaultCreateWarningAndErrorValidatorForViewModelValue = false; 

     IoCConfiguration.DefaultServiceLocator.AutoRegisterTypesViaAttributes = true; 
     IoCConfiguration.DefaultServiceLocator.CanResolveNonAbstractTypesWithoutRegistration = true; 

     foreach (
     var file in 
      BaseDirectory.GetFiles("*.dll", SearchOption.AllDirectories) 
      .Where(f => IsNetAssembly(f.FullName)) 
      .Where(f => !f.FullName.EndsWith("resources.dll")) 
      .AsParallel()) 
     { 
     try 
     { 
      var asm = Assembly.ReflectionOnlyLoadFrom(file.FullName); 
     } 
     catch { } 
     }

然後,我們嘗試對它們進行初始化我們有在AppDomain中加載的插件程序集,我們沒有得到所有具有ServiceLocatorRegistration的類貢。

是否有任何解決所有具有上述示例屬性設置的類? 在此先感謝!

回答

0

問題可能是因爲包含使用註冊的類型的程序集尚未加載到AppDomain中。有幾件事情你可以考慮:

1)使用AppDomainExtensions.PreloadAssemblies

2)以某種方式使用類型(如Console.WriteLine(typeof運算(TypeFromAssembly).FullName))

我不會推薦第二個是因爲它違背了你的插件架構。

+0

我會嘗試儘快(第一次),但正如我寫的 - 我們已經加載到默認的AppDomain程序集。 我在上面的問題中添加了相應的代碼。 問題可能是我們加載了組件「ReflectionOnly」? – chrisih

1

我自己解決了這個問題。錯誤可能是該行

var asm = Assembly.ReflectionOnlyLoadFrom(file.FullName);
AppDomain.CurrentDomain.LoadAssemblyIntoAppDomain(file.FullName);
替換此行後,一切都按預期工作。 感謝Geert指出正確的方向!