例如,如何使用 Ninject 将构造函数参数绑定到位图?

发布于 2024-09-15 03:59:51 字数 648 浏览 0 评论 0原文

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

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)
            ;
    }
}

I currently have a class of this form:

class Abc {
    private readonly IDisposable disposable;

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

    ...
}

Now, I'd like to know how can I make a binding of IDisposable to Bitmap using the

Bitmap(int widht, int height)

constructor.

I've tried with the following piece of code, but it doesn't seem to do it:

class TestModule : NinjectModule {

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2024-09-22 03:59:51

哦,这很简单:

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

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

Doh, this was an easy one:

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

will work, for example. There are a couple of other ways of doing it, though. They are all in the Bind() return object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文