简体   繁体   English

为什么Unity找不到我的参数化构造函数?

[英]Why does Unity not find my parameterized constructor?

My tiny, novice, Bootstrapper configures the container as follows: 我很小的新手Bootstrapper将容器配置如下:

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>()));
}

and my BaseViewModel has only one ctor: 我的BaseViewModel只有一个ctor:

public abstract class BaseViewModel : BindableBase, INotifyPropertyChanged
{
    protected BaseViewModel(AppState appState)
    {
        AppState = appState;
    }
    ...
}

When I try and run the project, the bootstrapper fails on RegisterType<BaseViewModel> and throws the following exception. 当我尝试运行该项目时,引导程序在RegisterType<BaseViewModel>上失败,并引发以下异常。

System.InvalidOperationException: 'The type ApptEase.Client.Infrastructure.ViewModels.BaseViewModel does not have a constructor that takes the parameters (AppState).' System.InvalidOperationException:'类型ApptEase.Client.Infrastructure.ViewModels.BaseViewModel没有采用参数(AppState)的构造函数。

AppState is created and registered with the container in the application startup code: 创建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);
}

Your BaseViewModel class constructor is protected. 您的BaseViewModel类构造函数受到保护。 Which is not visible to Unity. 这对Unity不可见。 But anyway, as user 3615 wrote in comments - 但是无论如何,正如用户3615在评论中写道-

You cannot resolve abstract class since instance cannot be created 您无法解析抽象类,因为无法创建实例

So I would suggest explicitly registering a concrete implementation instance and mapping it to that base type. 因此,我建议显式注册一个具体的实现实例并将其映射到该基本类型。

Container.RegisterInstance(typeof(BaseViewModel), new MyConcreteViewModel());

But in the end what you want is actually an interface typed registration. 但是最后,您实际上想要的是接口类型的注册。 That unlocks testability via mocking. 通过模拟解锁可测试性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在添加参数化构造函数时默认构造函数不起作用? - Why default constructor does not work when parameterized constructor added? 反射不会触发参数化构造函数 c# - Reflection does not trigger parameterized constructor c# 为什么Unity在解析类时无法选择String构造函数? - Why does Unity fail to select a String constructor when resolving class? 如何在C#中的统一容器中注册基于参数化的构造函数类? - how to register parameterized based constructor classes in unity container in C#? 我应该如何在Unity中注册这个具有参数化构造函数的类型? - How should I register this type that has parameterized constructor, in Unity? 为什么我的代码无法在Unity中运行? Unity C# - Why does my code not run in Unity? Unity C# 为什么音频在我的代码统一中不起作用? 统一 c# - Why does Audio not Work in my code unity ? unity c# 为什么我的数组改变时我的 int 会改变? (统一) - Why does my int change when my array changes? (Unity) 为什么我的Unity C#脚本中的变量重置为0 - Why does my variable reset to 0 in my Unity C# script ASPX-为什么调用无参数构造函数,而跳过带参数的构造函数? - ASPX - Why is the parameterless constructor invoked, skipping the parameterized one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM