【问题标题】:How, for example, to make a binding with constructor arguments to a Bitmap with Ninject?例如,如何使用 Ninject 将构造函数参数绑定到位图?
【发布时间】:2011-03-30 06:42:51
【问题描述】:

我目前有一个这种形式的课程:

class Abc {
    private readonly IDisposable disposable;

    public Abc(IDisposable disposable) {
        this.disposable = disposable;
    }

    ...
}

现在,我想知道如何使用

IDisposable 绑定到 Bitmap
Bitmap(int widht, int height)

构造函数。

我已尝试使用以下代码,但似乎没有:

class TestModule : NinjectModule {

    public override void Load()
    {
        Bind<IDisposable>().To<Bitmap>()
            .WithConstructorArgument("width", 10)
            .WithConstructorArgument("height", 22)
            ;
    }
}

【问题讨论】:

    标签: c# dependency-injection ioc-container ninject


    【解决方案1】:

    Doh,这很简单:

    Bind<IDisposable>().ToConstant(new Bitmap(10, 22));
    

    例如会起作用。不过,还有其他几种方法可以做到这一点。它们都在 Bind() 返回对象中。

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多