首页 > 解决方案 > PaymentManager 类型的构造函数包含名称为“paymentMethods”和类型为 List 的参数未注册的

问题描述

我也收到以下错误。这是因为它不是列表,如果是这样,我该如何纠正?

container.RegisterCollection<IPaymentMethod>(new[]
{
    typeof(AuthorizeNetProvider),
    typeof(StripeProvider),
    typeof(PayPalProProvider),
    typeof(PayPalStandardProvider),
    typeof(IntuitProvider),
    typeof(UsaEpayProvider),
    typeof(ITransactProvider),
    typeof(SecureNetProvider),
    typeof(ExposurePayProvider),
    typeof(PayTraceProvider),
    typeof(BraintreeProvider)
});

错误

配置无效。创建 IDivisionsService 类型的实例失败。PaymentManager 类型的构造函数包含名为“paymentMethods”的参数和未注册的 List<IPaymentMethod> 类型。请确保 List<IPaymentMethod> 已注册,或更改 PaymentManager 的构造函数。

承包商

public PaymentManager(List<IPaymentMethod> paymentMethods)
{
    _paymentMethods = paymentMethods;
}

标签: c#dependency-injectionsimple-injector

解决方案


List<T>目前支持作为集合类型。将构造函数更改为以下之一:

  • IEnumerable<T>
  • IList<T>
  • ICollection<T>
  • IReadOnlyList<T>
  • IReadOnlyCollection<T>
  • T[]

前 5 种类型具有以下行为:

  • 它们表现为,这意味着它们每次迭代时都会从容器中解析实例。
  • 正因为如此,它们被注入为单例。然而,他们的情况仍然根据他们适当的生活方式得到解决。
  • 它们是不可变的。尝试添加、更改或删除实例将会失败IList<T>ICollection<T>出现异常。

最后一种类型,数组,总是表示实例的副本,不会像流一样运行。因为它是一个可变的实例列表,所以数组总是被注入为Transient,即使它的依赖列表都代表Singleton实例。


推荐阅读