LightInject - 派生接口导致多个实例

时间:2016-06-13 14:09:45

标签: c# light-inject

在我的应用程序中,我处理了许多ViewModel,它们通过几个接口在LightInject Container中注册。其中一些接口来自其他用于UnitTesting目的。

当使用相同的接口解析多个ViewModel时,我得到的ViewModelinstance比预期的要多。

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

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?
    }
}

}

1 个答案:

答案 0 :(得分:1)

试试这个 var container = new ServiceContainer(new ContainerOptions() {EnableVariance = false});

相关问题