使用 C# using 语句延迟实例化

发布于 2024-10-11 13:14:45 字数 380 浏览 7 评论 0原文

有没有办法编写 using 语句而不立即实例化 IDisposable

例如,如果我需要做类似的事情:

using (MyThing thing)
{
    if (_config == null)
    {
         thing = new MyThing();
    }
    else
    {
         thing = new MyThing(_config);
    }

    // do some stuff

} // end of 'using'

对于这样的情况是否有可接受的模式?或者我是否再次显式处理 IDisposable

Is there any way to write a using statement without instantiating the IDisposable immediately?

For example, if I needed to do something like:

using (MyThing thing)
{
    if (_config == null)
    {
         thing = new MyThing();
    }
    else
    {
         thing = new MyThing(_config);
    }

    // do some stuff

} // end of 'using'

Is there an accepted pattern for cases like this? Or am I back to handling the IDisposable explicitly again?

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

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

发布评论

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

评论(4

你是暖光i 2024-10-18 13:14:45

好吧,在您的示例中,您确实立即实例化一次性对象 - 仅基于条件。例如,您可以使用:

using (MyThing thing = _config == null ? new MyThing() : new MyThing(_config))
{
    ...
}

为了更通用,您可以使用一种方法:

using (MyThing thing = CreateThing(_config))
{
}

棘手的一点是实例化的时间是否根据各种条件而改变。这确实会更难使用 using 语句来处理,但也会建议您应该尝试重构代码以避免该要求。这并不总是可行,但值得尝试。

另一种替代方法是将“事物”封装在包装器中,该包装器将适当地延迟创建真正的一次性对象,并委托该对象进行处置以及您可以对该类型执行的任何其他操作。在某些情况下,这样的委派可能会很痛苦,但它可能是合适的 - 取决于您真正想要做什么。

Well, in your example you do instantiate the disposable object immediately - just based on a condition. For example, you could use:

using (MyThing thing = _config == null ? new MyThing() : new MyThing(_config))
{
    ...
}

To be more general, you can use a method:

using (MyThing thing = CreateThing(_config))
{
}

The tricky bit would be if the timing of the instantiation changed based on various conditions. That would indeed be harder to handle with a using statement, but would also suggest that you should try to refactor your code to avoid that requirement. It won't always be possible, but it's worth trying.

Another alternative is to encapsulate the "thing" in a wrapper which will lazily create the real disposable object appropriately, and delegate to that for disposal and anything else that you can do with the type. Delegation like this can be a pain in some situations, but it might be appropriate - depending on what you're really trying to do.

深白境迁sunset 2024-10-18 13:14:45
using (MyThing thing = _config == null ? new MyThing() : new MyThing(_config))
{
   // ....

}
using (MyThing thing = _config == null ? new MyThing() : new MyThing(_config))
{
   // ....

}
尘曦 2024-10-18 13:14:45

你可以这样做:

if (_config == null)
{
     thing = new MyThing();
}
else
{
     thing = new MyThing(_config);
}

using (thing)
{

    // do some stuff
}

You could do:

if (_config == null)
{
     thing = new MyThing();
}
else
{
     thing = new MyThing(_config);
}

using (thing)
{

    // do some stuff
}
独守阴晴ぅ圆缺 2024-10-18 13:14:45

我认为最明智的解决方案是将配置的决定移至 MyThing 构造函数中。这样你就可以简化类的使用,如下所示:

using (MyThing thing = new MyThing(_config))
{

} 

class MyThing {
  public MyThing() {
    //default constructor
  }

  public MyThing(Config config) :this() {
    if (config == null)
    {
         //do nothing, default constructor did all the work already
    }
    else
    {
         //do additional stuff with config
    }
  }
}

I think the most sane solution is to move the decision of what to with the config into the MyThing constructor. That way you could simplify the usage of the class like so:

using (MyThing thing = new MyThing(_config))
{

} 

class MyThing {
  public MyThing() {
    //default constructor
  }

  public MyThing(Config config) :this() {
    if (config == null)
    {
         //do nothing, default constructor did all the work already
    }
    else
    {
         //do additional stuff with config
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文