'object'不包含'Dispose'的定义 [英] 'object' does not contain a definition for 'Dispose'

查看:894
本文介绍了'object'不包含'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 an IDisposable interface.

Implementing IDisposable and the Dispose Pattern Properly[^] should help you.


这篇关于'object'不包含'Dispose'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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