Autofac解析已注册组件类型的列表

时间:2016-03-08 21:25:10

标签: c# autofac

是否可以将组件注册为类型,然后获取所有类型的列表?

含义:

public abstract class MyObject
{
    ..
}

public class object1 : MyObject {}
public class object2 : MyObject {}
public class object3 : MyObject {}
public class object4 : MyObject {}

public class Root : IRoot
{
    public Root(IObjectsHolder objectsHolder)
}

public class ObjectsHolder  : IObjectsHolder
{
    // This list should contain: (typeof(object1), typeof(object2))
    public ObjectsHolder(List<Type> AllTypesThatInheritMyObject)
}

builder.RegisterType<object1>().As(context => context.Resolve<IObjectsHolder>().Deserialize<object1>().InstancePerlifetimeScope();
builder.RegisterType<object2>().As(context => context.Resolve<IObjectsHolder>().Deserialize<object2>().InstancePerlifetimeScope();


builder.RegisterType<Root>().As<IRoot>().SingleInstance();
builder.RegisterType<ObjectsHolder>().As<IObjectsHolder>().SingleInstance();

var container builder.Build();

container.Resolve<IRoot>();

我希望以某种方式注册所有MyObjects继承,以便ObjectsHolder获取其类型列表。这有可能吗?

0 个答案:

没有答案