无法将自定义类型隐式转换为 IDisposable 错误
我有这段代码:
try
{
using(conn)
{
conn.UpdateScheduledTaskGuid(taskID, taskGUID);
}
}
在 conn 变量上,我收到错误“无法将 DataProvider 类型隐式转换为 System.IDisposable”
DataProvider 是一个自定义类,我正在工作的这个项目中创建的人是上面 conn 变量的类型。
我想我需要让 DataProvider 实现 IDisposable,但我不确定这是否真的是问题所在,如果我确实必须实现它,那么它有哪些非托管资源存在问题,需要我添加 IDisposable?
以下是 DataProvider 类:DataProvider.txt
I've got this piece of code:
try
{
using(conn)
{
conn.UpdateScheduledTaskGuid(taskID, taskGUID);
}
}
On the conn variable I'm getting the error "Cannot implicitly convert type DataProvider to System.IDisposable"
DataProvider is a custom class that someone created in this project I'm working in which is the type for the conn variable above.
I guess I need to have DataProvider implement IDisposable but I am not sure if that's really the problem here or not and if I do have to implement it, what unmanaged resources is it having a problem with that requires me to add IDisposable?
Here's the DataProvider class:DataProvider.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自有关使用声明的 MSDN 页面。
和
所以是的,您需要让 DataProvider 实现 IDisposable 才能像在 using 块中一样使用它。
From the MSDN page about the using Statement.
and
So yes, you will need to have your DataProvider implement IDisposable to use it as you are in a using block.
DataProvider
确实必须实现IDisposable
,因为它拥有一个IDbConnection
,它必须正确处置它。DataProvider
must indeed implementIDisposable
, because it owns aIDbConnection
, which it must properly dispose of.“using”关键字只是编写异常安全代码的一种有用方法,当变量超出范围时,该代码将调用 Dispose()。如果您不打算调用 Dispose(),则它没有用。该函数由 IDisposable 接口声明。
如果您没有非托管资源,则无论如何您都不应该对其使用“using”。
对此的一个很好的描述在这里:
http://www.codeproject.com/KB/cs/using_and_IDisposable.aspx
The "using" keyword is just a helpful way of writing exception-safe code that will call Dispose() when the variable goes out of scope. If you're not going to call Dispose(), it's not useful. That function is declared by the IDisposable interface.
If you don't have unmanaged resources, you shouldn't be using "using" on it, anyway.
A good description of this is here:
http://www.codeproject.com/KB/cs/using_and_IDisposable.aspx
是的,为了使用“using”语句,对象需要实现 IDisposable。我查看了一些代码,没有看到任何非托管代码(尽管我可能会弄错,因为那里有大量代码)。
Yes, in order to use the 'using' statement the object needs to implement IDisposable. I looked through a bit of that code and I didn't see any unmanaged code (although I could be mistaken since there is a ton of code there).