C#Ninject to Structure map converter

时间:2017-02-16 12:18:24

标签: c# dependency-injection ninject inversion-of-control structuremap

ninject我有这样的代码:

var resourceManagers = new ResourceManager[1];
resourceManagers[0] = Validation.ResourceManager;
kernel.Bind<ILocalizedStringProvider>().To<ResourceStringProvider>()
    .WithConstructorArgument("resourceManager", resourceManagers);

kernel.Rebind<ModelValidatorProvider>().To<LocalizedModelValidatorProvider>();

我想将其转换为StructureMap

我确实喜欢这个:

IContainer container = new Container();
var ioC = new IoC();
ioC.Initialize(container);

container.Configure(x =>
{

    var resourceManagers = new ResourceManager[1];
    resourceManagers[0] = ModelValidation.ResourceManager;
    x.For<ILocalizedStringProvider>().Use<ResourceStringProvider>.Ctor<string>(@"resourceManager").Is(resourceManagers);
    x.For<ModelValidatorProvider>().Add<LocalizedModelValidatorProvider>();
});

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);

但我得到例外:

  

严重级代码描述项目文件行抑制状态   错误CS0119'CreatePluginFamilyExpression.Use()'   是一种方法,在给定的上下文中无效

如何使用StructureMap

执行此操作

2 个答案:

答案 0 :(得分:1)

正如错误消息所示,这:

.Use<ResourceStringProvider>

应该是:

.Use<ResourceStringProvider>()

因为这是一种方法。

答案 1 :(得分:1)

使用()因为是方法

var resourceManagers = new ResourceManager[1];
resourceManagers[0] = ModelValidation.ResourceManager;

x.For<ILocalizedStringProvider>().Use<ResourceStringProvider>().Ctor<ResourceManager[]> (@"resourceManager").Is(resourceManagers);
x.For<ModelValidatorProvider>().Add<LocalizedModelValidatorProvider>();