0

此代码不起作用。我基本上有一些超接口和子接口的实现。我希望超级接口返回所有实现,子接口只返回子实现。我不想明确地将子实现绑定到子接口和超级接口。我只想将它绑定到子接口并以某种方式将子绑定到超级。我正在尝试像这样设置它,但它不起作用:

class Program
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        kernel.Bind<ISuperInterface>().To<ISubInterface>(); // What can I do instead of this?
        kernel.Bind<ISuperInterface>().To<SuperImplementation>();
        kernel.Bind<ISubInterface>().To<SubImplementation>();

        var subs = kernel.GetAll<ISubInterface>(); // I want this to return a SubImplementation
        var supers = kernel.GetAll<ISuperInterface>(); // I want this to return a SuperImplementation and a SubImplementation

        Console.WriteLine(subs.Count());
        Console.WriteLine(supers.Count());
    }
}

public class SubImplementation : ISubInterface
{
}

public class SuperImplementation : ISuperInterface
{
}

public interface ISuperInterface
{
}

public interface ISubInterface : ISuperInterface
{
}
4

1 回答 1

0

Ninject 支持接口隔离:

kernel.Bind<ISuperInterface>().To<SuperImplementation>();
kernel.Bind<ISuperInterface, ISubInterface>().To<SubImplementation>();

或者,如果您正在使用约定,您可以使用BindAlInterfaces

于 2012-11-03T07:11:51.737 回答