在调用 Dispose() 之前转换为 IDisposable
在调用 Dispose() 之前强制转换为 IDisposable 的原因是什么?
public interface ITransaction : IDisposable
{}
.
.
.
//in some other class:
public void EndTransaction(ITransaction transaction)
{
if (transaction != null)
{
(transaction as IDisposable).Dispose();
// is the following code wrong? transaction.Dispose()
transaction = null;
}
}
这是 ITransaction 的具体实现之一:
public class NHibernateTransaction : ITransaction
{
public NHibernateTransaction(NHibernate.ITransaction transaction)
{
this.Transaction = transaction;
}
protected NHibernate.ITransaction Transaction { get; private set; }
public void Dispose()
{
if ( this.Transaction != null )
{
(this.Transaction as IDisposable).Dispose(); // this is NHibernate ITransaction object
this.Transaction = null;
}
}
}
我在存储库模式的开源实现中多次看到该代码片段,但我似乎无法理解转换背后的原因。在 if 子句 内直接调用 transaction.Dispose() 应该可以正常工作。我错过了什么吗?
原始代码可以在这里找到: NHibernateTransaction.cs
What is the reason of casting to IDisposable before calling Dispose() ?
public interface ITransaction : IDisposable
{}
.
.
.
//in some other class:
public void EndTransaction(ITransaction transaction)
{
if (transaction != null)
{
(transaction as IDisposable).Dispose();
// is the following code wrong? transaction.Dispose()
transaction = null;
}
}
This is one of the concrete implementation of ITransaction:
public class NHibernateTransaction : ITransaction
{
public NHibernateTransaction(NHibernate.ITransaction transaction)
{
this.Transaction = transaction;
}
protected NHibernate.ITransaction Transaction { get; private set; }
public void Dispose()
{
if ( this.Transaction != null )
{
(this.Transaction as IDisposable).Dispose(); // this is NHibernate ITransaction object
this.Transaction = null;
}
}
}
I have seen that code snippet many times in an open source implementation of the repository pattern and I cannot seem to understand the reason behind the cast. Directly calling transaction.Dispose() inside the if clause should work just fine. Did I miss something?
The original code can be found here:
NHibernateTransaction.cs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
ITransaction
继承自IDisposable
,因此实现者可能已将IDisposable
实现为 显式接口实现,在这种情况下,需要进行强制转换才能访问已实现的成员。在这种情况下,转换可确保调用将调用
IDisposable.Dispose
方法。演员阵容已覆盖所有基地。如果
ITransaction
不是从IDisposable
继承的,但实现者继承了,则需要进行强制转换才能调用Dispose
。如果实现者没有实现IDisposable
,这种情况可能会失败(抛出异常)。Since
ITransaction
inherits fromIDisposable
, it is possible that the implementer has implementedIDisposable
as an explicit interface implementation, in which case the cast is required in order to access the implemented members.In such a case, casting ensures that the call will call the
IDisposable.Dispose
method. The cast is done to cover all bases.If
ITransaction
doesn't inherit fromIDisposable
, but the implementer does, a cast is needed forDispose
to be callable. Such a case may fail (throwing an exception) if the implementer does not implementIDisposable
.在派生自 IDisposable 的接口上调用
Dipose()
时,没有区别。在实现
IDisposable
的类上调用Dispose()
时可能会有所不同,因为Dispose()
方法可能是显式实现的。When calling
Dipose()
on an interface deriving from IDisposable, there is no difference.There could be a difference when calling
Dispose()
on a class implementingIDisposable
, because the methodDispose()
may be implemented explicitly.我不经常这样做,但当我理解奥德现在想说什么时,我修改了我原来的整个答案。他们的目的是解决这个问题:
“transaction.Dispose()”调用什么?它调用 IDisposable.Dispose()
但问题是,由于 EndTransaction 的契约是 ITransaction,因此它将始终调用 IDisposable 版本。
I don't do this often but I whacked my whole original answer as I understand what Oded is trying to say now. Their intent is to solve this problem:
What does 'transaction.Dispose()' invoke? It invokes
IDisposable.Dispose()
The problem though, is that since the contract into EndTransaction is ITransaction, it will always invoke the IDisposable version.