我的小,新手,Bootstrapper
配置容器如下:爲什麼Unity找不到我的參數化構造函數?
protected override void ConfigureContainer()
{
base.ConfigureContainer();
RegisterTypeIfMissing(typeof(IUserSettingsProvider), typeof(JsonUserSettingsProvider), true);
RegisterTypeIfMissing(typeof(IAppState), typeof(AppState), true);
Container.RegisterType<TripleDesEncryptionService>(new InjectionConstructor(App.CryptoKey));
Container.RegisterType<BaseViewModel>(new InjectionConstructor(Container.Resolve<IAppState>()));
}
和我BaseViewModel
只有一個構造函數:
public abstract class BaseViewModel : BindableBase, INotifyPropertyChanged
{
protected BaseViewModel(AppState appState)
{
AppState = appState;
}
...
}
當我嘗試並運行該項目,引導程序上RegisterType<BaseViewModel>
和失敗拋出以下異常。
System.InvalidOperationException: '的 類型ApptEase.Client.Infrastructure.ViewModels.BaseViewModel沒有 一個構造函數的參數(屆時AppState)'。
AppState
被創建並在應用程序啓動代碼的容器中註冊:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var boot = new Bootstrapper();
boot.Run();
Container = boot.Container;
_appState = new AppState();
Container.RegisterInstance(typeof(AppState), _appState);
}
你不能解析抽象類,因爲無法創建實例 – 3615
加上它沒有公共構造函數... –
@ 3615我不想解析抽象類,但我想要ctor注入它的所有子類。 – ProfK