簡體   English   中英

NInject相當於Autofac的AsClosedTypesOf

[英]NInject equivalent of Autofac's AsClosedTypesOf

以下是使用Autofac的代碼的NInject等效項:

var builder = new ContainerBuilder();

System.Reflection.Assembly assembly = ...;
builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(OpenGeneric<>))
                                       .As<IAnInterface>();

var resolved = container.Resolve<IEnumerable<IAnInterface>>();

使用Ninject 3.0.0-rc3可以使用

kernel.Bind(
      x => x.FromThisAssembly()
            .SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric()
            .BindToAllInterfaces());

根據您的要求,您可能會刪除WhichAreGeneric語句。 .SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric()選擇要創建綁定的類。

約定確保接口和實現類必須具有相同的開放類型參數。 例如萬一

interface IBar<T1, T2>
interface IBaz<T>
interface IFoo
class Bar<T1, T2> : IBar<T1, T2>, IBaz<T1>, IFoo
class Foo : IBar<int, int>, IFoo

IBar<T1, T2>Bar<T1, T2>的唯一有效接口。 但是對於Foo來說, IBar<int, int>, IFoo都是有效的。

暫無
暫無

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

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