C# LightInject派生的接口导致多个实例

C# LightInject派生的接口导致多个实例,c#,light-inject,C#,Light Inject,在我的应用程序中,我处理许多ViewModels,这些ViewModels通过几个接口在LightInject容器中注册。出于单元测试的目的,其中一些接口是从其他接口派生的 当使用同一界面解析多个ViewModel时,我得到的ViewModelinstances比预期的多 我为这种行为做了一个简化的例子。有可能以某种方式防止这种行为吗 using System; using System.Collections.Generic; using System.Linq; using System.T

在我的应用程序中,我处理许多ViewModels,这些ViewModels通过几个接口在LightInject容器中注册。出于单元测试的目的,其中一些接口是从其他接口派生的

当使用同一界面解析多个ViewModel时,我得到的ViewModelinstances比预期的多

我为这种行为做了一个简化的例子。有可能以某种方式防止这种行为吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
public interface IBar
{
}

public interface IFoo : IBar
{
}

public class Cat : IFoo
{
}

class Program
{
    static void Main(string[] args)
    {
        var container = new LightInject.ServiceContainer();

        container.Register<IBar, Cat>();
        container.Register<IFoo, Cat>();

        var m = container.GetAllInstances(typeof(IBar));

        // m will contain 2 Instances of Cat. Is it possible it will resolve only 1 Instance?
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
公共接口IBar
{
}
公共接口IFoo:IBar
{
}
公共类猫:IFoo
{
}
班级计划
{
静态void Main(字符串[]参数)
{
var container=new LightInject.ServiceContainer();
container.Register();
container.Register();
var m=container.GetAllInstances(typeof(IBar));
//m将包含2个Cat实例。它是否可能只解析1个实例?
}
}
}试试这个

var-container=new-servicecainer(new-ContainerOptions(){enablevance=false});

这似乎可以解决它:)