Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将StructureMap与派生接口一起使用_C#_Interface_Structuremap_Instances - Fatal编程技术网

C# 将StructureMap与派生接口一起使用

C# 将StructureMap与派生接口一起使用,c#,interface,structuremap,instances,C#,Interface,Structuremap,Instances,我有一个类似于以下内容的对象层次结构: interface IVideoStream { } abstract class VideoStream : IVideoStream { } interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ } interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ } class V

我有一个类似于以下内容的对象层次结构:

interface IVideoStream { }

abstract class VideoStream : IVideoStream { }

interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ }

interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ }

class VideoStreamTypeA : IVideoStreamTypeA { }

class VideoStreamTypeB : IVideoStreamTypeB { }
Forward<IVideoStreamTypeA, IVideoStream>();
Forward<IVideoStreamTypeB, IVideoStream>();
VideoStreamTypeA和VideoStreamTypeB都应该有一个实例,因为它们包装了一些资源。有些类直接使用IVideoStreamTypeA或IVideoStreamTypeB,有些类使用IVideoStream列表

寄存器代码如下所示:

class MyRegistry: Registry
{
    public MyRegistry()
    {
        For<IVideoStreamTypeA>().Use<VideoStreamTypeA>()
            .Ctor<>() // Specific initialization
        For<IVideoStreamTypeB>().Use<VideoStreamTypeB>()
            .Ctor<>() // Specific initialization

        For<IVideoStreamTypeA>().Singleton();
        For<IVideoStreamTypeB>().Singleton();
    }
}
最后,有一些类获取了IVideoStream的列表:

class MyClass 
{
    public MyClass(IEnumerable<IVideoStream> streams) { }
}

对于当前注册表代码,streams参数为空。如何让StructureMap从上面注入两个实例?

我当前的方法是使用以下方法:

interface IVideoStream { }

abstract class VideoStream : IVideoStream { }

interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ }

interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ }

class VideoStreamTypeA : IVideoStreamTypeA { }

class VideoStreamTypeB : IVideoStreamTypeB { }
Forward<IVideoStreamTypeA, IVideoStream>();
Forward<IVideoStreamTypeB, IVideoStream>();

但我不确定这是不是最好的解决方案

我认为这是唯一的解决方案。