簡體   English   中英

Autofac注冊錯誤:類型'System.Collections.IEnumerable'不是開放的通用類或接口類型

[英]Autofac registration error: The type 'System.Collections.IEnumerable' is not an open generic class or interface type

我嘗試向IPage<>注冊IPage<>接口,但是它總是拋出以下異常:

類型“ System.Collections.IEnumerable”不是開放的通用類或接口類型。

這是我的代碼:

containerBuilder.RegisterAssemblyTypes(assembly)
                .Where(t => t.IsAssignableFrom(typeof(IList<>)))
                .AsSelf()
                .AsImplementedInterfaces();

我的界面和實現:

public interface IPage<T> : IList<T>
{
}

public interface Page<T> : List<T>, IPage<T>
{
}

您能幫助我理解為什么會發生這種情況以及如何解決嗎?

IList<T>繼承自IEnumerable 在您的類型上調用.AsImplementedInterfaces()就像調用.AsImplementedInterfaces() .As<IList<T>>().As<IPage>().As<IEnumerable<T>>().As<ICollection<T>>().As<IEnumerable>()不確切知道為什么,但是Autofac嘗試將IEnumerable注冊為封閉類型,但失敗。

如果要解析IPage<T> ,則不必使用.AsImplementedInterfaces()注冊類型, .AsImplementedInterfaces()必須將其注冊為封閉類型:

builder.RegisterAssemblyTypes(assembly)
       .AsClosedTypesOf(typeof(IPage<>));

然后,當您解析IPage<Customer>Autofac將為您提供Page<Customer>實例

嘗試這樣的事情:

builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(IList<>));

AsClosedTypesOf指定如果掃描的程序集實現關閉提供的打開的通用接口類型的接口,則將注冊該程序集的類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM