我有一个现有的程序,其中一些插件基础设施目前依赖于具有无参数构造函数的插件类。我想为插件作者提供一个机会,通过为构造函数指定参数,从我的程序中只需要一些基础设施。在内部,我使用一些通用的包装器类来封装插件的类,并使它们对我的程序的其余部分的行为像旧的前插件时代的内部类。
我在这里有一些占位符代表我的基础结构:
public interface IInfrastructure
{
}
public class Infrastructure : IInfrastructure
{
}
一些插件接口规范:
public interface IPlugin
{
}
需要我的基础结构的插件实现:
public class Plugin : IPlugin
{
public Plugin(IInfrastructure _)
{
}
}
和我的通用包装器类期望一些插件类
public class PluginWrapper<TImpl> where TImpl: class, IPlugin
{
public PluginWrapper(TImpl _)
{
}
}
注册相关类型后:
ServiceLocator.Default.RegisterType<IInfrastructure, Infrastructure>(RegistrationType.Transient);
ServiceLocator.Default.RegisterType(typeof(Plugin),typeof(Plugin), RegistrationType.Transient);
var wrapperType = typeof(PluginWrapper<>).MakeGenericType(typeof(Plugin));
ServiceLocator.Default.RegisterType(wrapperType, wrapperType,RegistrationType.Transient);
我发现我可以解决内部"插件类型:Assert.NotNull(ServiceLocator.Default.ResolveType<Plugin>());
但我无法解决"包装"问题。类型。Assert.NotNull(ServiceLocator.Default.ResolveType<PluginWrapper<Plugin>>());
我是否达到了Catel的IoC容器的限制,或者我做错了什么?
当不使用通用注册方法时,我将注册类型传递到"标签">
因此,将注册部分更改为以下版本:
ServiceLocator.Default.RegisterType<IInfrastructure, Infrastructure>(RegistrationType.Transient);
ServiceLocator.Default.RegisterType(typeof(Plugin),typeof(Plugin),registrationType:RegistrationType.Transient);
var wrapperType = typeof(PluginWrapper<>).MakeGenericType(typeof(Plugin));
ServiceLocator.Default.RegisterType(wrapperType, wrapperType, registrationType:RegistrationType.Transient);
修复了这个问题,一切都如预期的那样进行。