Register/Resolve 使用带有 Castle Windsor 的开放通用祖先类型的部分封闭通用后代
Register/Resolve partially closed generic descendant using open generic ancestor type with Castle Windsor
与祖先class:
abstract class MyOpenGenericAncestor<T, TOptions>
和后代 class:
class MyPartiallyClosedDescendant<T> : MyOpenGenericAncestor<T, MyConcreteOptionsType>
我希望能够使用 MyOpenGenericAncestor 解析 MyPartiallyClosedDescendant,例如:
MyOpenGenericAncestor<T, TOptions> ResolveGeneric<T, TOptions>(TOptions options)
{
// assume options are used for stuff
return _container.Resolve<MyOpenGenericAncestor<T, TOptions>();
}
有没有办法做到这一点?在此代码的当前实现中,注册如下所示:
var assemblyFilter = new AssemblyFilter("C:\thePath\", "MyStuff.*.dll");
var myTypes = Classes
.FromAssemblyInDirectory(assemblyFilter)
.BasedOn(typeof(MyOpenGenericAncestor<,>))
.WithServiceBase()
.LifestyleTransient();
container.Register(myTypes);
通过此注册,MyPartiallyClosedDescendant 被注册为一个组件,但调用 ResolveGeneric(myConcreateOptionsInstance) 会导致 Castle.MicroKernel.ComponentNotFoundException 并显示消息,没有找到支持服务 MyOpenGenericAncestor`2[MyConcreateTType, MyConcreateOptionsType] 的组件.获取该异常是有道理的,但是有没有办法通过请求祖先来注册 and/or 解析后代?
在Castle中,有IGenericImplementationMatchingStrategy就是为了这个目的。不幸的是,我不确定如何在会议注册中使用它,所以至少这个...
public class MyOpenGenericAncestorMatchingStrategy : IGenericImplementationMatchingStrategy
{
public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
{
return new[] { context.GenericArguments[1] };
}
}
container.Register(Component.For(typeof(MyOpenGenericAncestor<,>))
.ImplementedBy(typeof(MyPartiallyClosedDescendant<>), new MyOpenGenericAncestorMatchingStrategy()));
与祖先class:
abstract class MyOpenGenericAncestor<T, TOptions>
和后代 class:
class MyPartiallyClosedDescendant<T> : MyOpenGenericAncestor<T, MyConcreteOptionsType>
我希望能够使用 MyOpenGenericAncestor 解析 MyPartiallyClosedDescendant,例如:
MyOpenGenericAncestor<T, TOptions> ResolveGeneric<T, TOptions>(TOptions options)
{
// assume options are used for stuff
return _container.Resolve<MyOpenGenericAncestor<T, TOptions>();
}
有没有办法做到这一点?在此代码的当前实现中,注册如下所示:
var assemblyFilter = new AssemblyFilter("C:\thePath\", "MyStuff.*.dll");
var myTypes = Classes
.FromAssemblyInDirectory(assemblyFilter)
.BasedOn(typeof(MyOpenGenericAncestor<,>))
.WithServiceBase()
.LifestyleTransient();
container.Register(myTypes);
通过此注册,MyPartiallyClosedDescendant 被注册为一个组件,但调用 ResolveGeneric(myConcreateOptionsInstance) 会导致 Castle.MicroKernel.ComponentNotFoundException 并显示消息,没有找到支持服务 MyOpenGenericAncestor`2[MyConcreateTType, MyConcreateOptionsType] 的组件.获取该异常是有道理的,但是有没有办法通过请求祖先来注册 and/or 解析后代?
在Castle中,有IGenericImplementationMatchingStrategy就是为了这个目的。不幸的是,我不确定如何在会议注册中使用它,所以至少这个...
public class MyOpenGenericAncestorMatchingStrategy : IGenericImplementationMatchingStrategy
{
public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
{
return new[] { context.GenericArguments[1] };
}
}
container.Register(Component.For(typeof(MyOpenGenericAncestor<,>))
.ImplementedBy(typeof(MyPartiallyClosedDescendant<>), new MyOpenGenericAncestorMatchingStrategy()));