对于多个接口返回相同的实例

6 浏览
0 Comments

对于多个接口返回相同的实例

我正在使用以下代码注册组件:

StandardKernel kernel = new StandardKernel();
string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location)
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    if (!Path.GetDirectoryName(assembly.Location).Equals(currentDirectory)) 
        continue;
    foreach (var type in assembly.GetTypes())
    {
        if (!type.IsComponent()) 
            continue;
        foreach (var @interface in type.GetInterfaces())
        kernel.Bind(@interface).To(type).InSingletonScope();
    }
}

然后我有一个实现了两个接口的类:

class StandardConsole : IStartable, IConsumer

如果我解析IStartable,我会得到一个实例;如果我解析IConsumer,我会得到另一个实例。

如何才能获得相同的实例来满足这两个接口?

0