'object'不包含'Dispose'的定义 [英] 'object' does not contain a definition for 'Dispose'
问题描述
我正在尝试使用以下代码来处理对象..
protected void Dispose(bool disposing)
{
if(disposing)
{if(components!= null)
{components.Dispose(); }
}
base.Dispose(disposing); }
但我收到错误
''对象''不包含定义''Dispose''
I am trying to use the following code to dispose the objects..
protected void Dispose( bool disposing )
{
if( disposing )
{ if(components != null)
{ components.Dispose(); }
}
base.Dispose( disposing ); }
but i am getting the error
''object'' does not contain a definition for ''Dispose''
推荐答案
替换行
Replace the line
base.Dispose( disposing );
by
by
IDisposable disposable = base as IDisposable;
if (disposable != null)
{
disposable.Dispose(disposing);
}
如果 base
类没有实现 IDisposable
,一次性
对象将为null - 请勿在此处调用 Dispose
。
你班上的方法应该是 public
或 public virtual
或 public覆盖
,不仅 protected
。
In case that your base
class does not implement IDisposable
, the disposable
object will be null - do not call Dispose
here.
And the method in your class should be public
or public virtual
or public override
, not only protected
.
您在Dispose中使用protected。因此,您需要确保对象继承Disposable对象。
这通常通过实现IDisposable
接口来完成。
正确实现IDisposable和Dispose模式 [ ^ ]应该帮助你。
You are using protected in Dispose. Thus you need to ensure object inherits a Disposable object.
This is normally done by implementing anIDisposable
interface.
Implementing IDisposable and the Dispose Pattern Properly[^] should help you.
这篇关于'object'不包含'Dispose'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!