通过引用传递IDisposable的对象会导致错误? [英] Passing an IDisposable object by reference causes an error?

查看:160
本文介绍了通过引用传递IDisposable的对象会导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用于处置对象实现的 IDisposable的,名为 DisposeObject()

I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject()

要确保我我处理由原始参考指出一个对象,我想通过引用传递的对象。

To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference.

但我正在逐渐写着

在'裁判'参数类型不匹配参数类型

The 'ref' argument type doesn't match parameter type

在下面的(简化)代码,无论 _Baz _Bar 实施的 IDisposable的

In the below (simplified) code, both _Baz and _Bar implement IDisposable.

所以,问题是,

So the questions are,


  1. 为什么会出现这个错误?

  2. 有没有办法来解决呢?

[更新]
从提供的答案让目前,只要我不设置IDisposable的参数为空,我可以简单地通过值,而不使用 REF 传递对象。
我现在有另一个麻烦是否 DisposeObject 方法中设置一次性对象与否。

[UPDATE] From provided answers so far, as long as I do not set an IDisposable argument to null, I can simply pass an object by value without using ref. I am now having another trouble whether to set disposable objects to null or not within DisposeObject method.

下面是完整的完整的源代码:

Here is the full source for completeness:

public class Foo : IDisposable
{
    private Bar _Bar;
    private Baz _Baz;
    private bool _IsDisposed;

    ~Foo() { Dispose(false); }

    public void Dispose(bool disposing)
    {
        if (!_IsDisposed)
        {
            if (disposing)
            {
                DisposeObject(ref _Baz);
                DisposeObject(ref _Bar);
            }
        }

        _IsDisposed = true;
    }

    private void DisposeObject(ref IDisposable obj)
    {
        try
        {
            if (obj == null) 
                return;
            obj.Dispose();
            obj = null;
        } catch (ObjectDisposedException) { /* Already Disposed... */ }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

public class Bar : IDisposable
{
    public void Dispose() {}
}

public class Baz : IDisposable
{
    public void Dispose() {}
}

[结果]

我删除了参数设置为null代码(的obj = NULL; )内的 DisposeObject
所以最终的代码变成了。

[RESULT]
I removed the code that sets argument to null (obj = null;) within DisposeObject So the final code became.

    public void Dispose(bool disposing)
    {
        if (!_IsDisposed)
        {
            if (disposing)
            {
                DisposeObject(_Baz);
                DisposeObject(_Bar);
            }
        }

        _IsDisposed = true;
    }

    private void DisposeObject(IDisposable obj)
    {
        try
        {
            if (obj == null) 
                return;
            obj.Dispose();
        } catch (ObjectDisposedException) { /* Already Disposed... */ }
    }


推荐答案

有没有必要让你按引用传递,为你传递一个引用类型。你应该从你的方法定义删除 REF 关键字。做到这一点,你不应该有任何的问题,虽然我不知道这是怎么不是简单地调用处置更有效更清晰或()(比其他事实,你没有投它明确的实现,这确实一个检查你)。

There is no need for you to pass by reference, as you're passing a reference type. You should remove the ref keyword from your method definition. Do this and you shouldn't have any issues, though I'm not sure how this is more effective or clearer than simply calling Dispose() (other than the fact that you don't have to cast it for explicit implementations and this does a null check for you).

修改

舞蹈,而我希望这是围绕着这个话题的讨论已经对您有所帮助,你的原意似乎并没有成为东西是可行的。为了通过一些如 REF ,你不能传递具有比其他类型的变量是什么 REF 参数期望(换句话说,你不能将声明为变量或其他接口实现的IDisposable 如果 REF 参数的IDisposable )。因为 REF 参数允许分配传播给调用者,你会打开允许不兼容的类型被存储在您的变量的可能性。

Dance, while I hope the discussion that's surrounded this topic has been helpful to you, your original intent doesn't seem to be something that's doable. In order to pass something as ref, you can't pass a variable that has a type other than what the ref parameter expects (in other words, you can't pass a variable declared as a class or other interface that implements IDisposable if the ref parameter is IDisposable). Because ref parameters allow assignments to propagate back to the caller, you would open up the possibility of allowing incompatible types being stored in your variable.

在这里最好的办法是分配自己,如果这就是你想要的。如果你想封装检查,并到这很好,但 REF 不会为工作中的作用而忽略例外你在这种情况下,无论你如何切它,很遗憾。

Your best bet here is to assign null yourself if that's what you want. If you want to encapsulate the null check and ignoring exceptions in to the function that's fine, but ref will not work for you in this scenario no matter how you slice it, unfortunately.

这篇关于通过引用传递IDisposable的对象会导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆