.net 当类型的构造函数未知时,如何创建Ninject提供程序?
我决定尝试召唤一个容器组件与之交互 FubuMVC。最酷的是它通过了所有的测试 FubuMVC.Container.StructureMap程序集不支持。然而,当我摔倒的时候 把它放进FubuSample。我收到一个激活错误 错误是因为在行为的提供程序中,我只是 调用无参数构造函数以生成行为实例。那个 这在现实生活中似乎是不可接受的 以下是它的设置方式:.net 当类型的构造函数未知时,如何创建Ninject提供程序?,.net,dependency-injection,ioc-container,ninject,fubumvc,.net,Dependency Injection,Ioc Container,Ninject,Fubumvc,我决定尝试召唤一个容器组件与之交互 FubuMVC。最酷的是它通过了所有的测试 FubuMVC.Container.StructureMap程序集不支持。然而,当我摔倒的时候 把它放进FubuSample。我收到一个激活错误 错误是因为在行为的提供程序中,我只是 调用无参数构造函数以生成行为实例。那个 这在现实生活中似乎是不可接受的 以下是它的设置方式: public class TestBehavior2 : IControllerActionBehavior { public ICo
public class TestBehavior2 : IControllerActionBehavior
{
public IControllerActionBehavior InsideBehavior { get; set; }
public IInvocationResult Result { get; set; }
protected FubuConfiguration Configuration { get; set; }
protected FubuConventions Conventions { get; set; }
public TestBehavior2(FubuConventions conventions, FubuConfiguration config)
{
Conventions = conventions;
Configuration = config;
}
public OUTPUT Invoke<INPUT, OUTPUT>(INPUT input, Func<INPUT, OUTPUT> func)
where INPUT : class
where OUTPUT : class
{
// Invocation stuff
}
}
public class TestBehavior : IControllerActionBehavior
{
public IControllerActionBehavior InsideBehavior { get; set; }
public IInvocationResult Result { get; set; }
public OUTPUT Invoke<INPUT, OUTPUT>(INPUT input, Func<INPUT, OUTPUT> func)
where INPUT : class
where OUTPUT : class
{
// Invocation stuff
}
}
所以我的问题是,我应该如何设置提供者来创建
一个服务的实例,当所有我不知道的构造函数将
看起来像或者更确切地说,如果我使用ConstructorInfo来确定
构造函数将注入适当的依赖项吗
这是使用Ninject 2b,因为FubuMvc需要CommonServiceLocator
支持 没关系,我发现我把事情弄得更复杂了。以下是我的新创建方法:
public object Create(IContext context)
{
IControllerActionBehavior behavior = context.Kernel.Get<DefaultBehavior>();
_config.GetBehaviors().Reverse().Each(t =>
{
var temp = behavior;
behavior = (IControllerActionBehavior) context.Kernel.Get(t);
t.GetProperty(INSIDE_PROP_NAME).SetValue(behavior, temp, null);
});
_type = behavior.GetType();
return behavior;
}
公共对象创建(IContext上下文)
{
IControllerationBehavior=context.Kernel.Get();
_config.GetBehaviors().Reverse().Each(t=>
{
var-temp=行为;
行为=(IControllerationBehavior)context.Kernel.Get(t);
t、 GetProperty(在属性名称中).SetValue(行为、临时、空);
});
_type=behavior.GetType();
回归行为;
}
我所要做的就是得到具体的行为类型,每个人都很高兴。你能提供一些示例代码吗?我不确定您到底是如何使用该提供程序的?请注意,如果可以的话,Ninject将解析提供程序构造函数中的任何参数。
public object Create(IContext context)
{
var behavior = ConfigureInstance(context);
foreach (var type in _config.GetBehaviors().Reverse())
{
IControllerActionBehavior previousBehavior = behavior;
behavior = (IControllerActionBehavior)Activator.CreateInstance(type);
t.GetProperty(INSIDE_PROP_NAME).SetValue(behavior, temp, null);
}
return behavior;
}
public object Create(IContext context)
{
IControllerActionBehavior behavior = context.Kernel.Get<DefaultBehavior>();
_config.GetBehaviors().Reverse().Each(t =>
{
var temp = behavior;
behavior = (IControllerActionBehavior) context.Kernel.Get(t);
t.GetProperty(INSIDE_PROP_NAME).SetValue(behavior, temp, null);
});
_type = behavior.GetType();
return behavior;
}