有一个祖先类:
abstract class MyOpenGenericAncestor<T, TOptions>
和子类:
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]服务的组件被找到。得到这个异常是有道理的,但是是否有一种方法可以通过请求祖先来注册和/或解决后代呢?