- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我当前的项目中,我使用的类实现了如下所示的 ITransaction
接口(interface)。这是可以撤消的事务的通用接口(interface)。我还有一个 TransactionSet
类,用于尝试多个事务或 TransactionSet,最终可用于创建事务树。
ITransaction
的某些实现会保留对对象实例或文件的临时引用,如果调用 Undo()
,它可能会在以后使用。稍后可以确认交易成功,之后不再允许 Undo()
,因此也不再需要临时数据。目前,我正在使用 Dispose()
作为清理所有临时资源的确认方法。
但是,现在我希望我的事务也能触发事件以通知其他类发生了什么。除非交易得到确认,否则我不希望事件触发。因为我不想允许事务通过撤消然后再次运行来多次触发事件。
由于我正在使用 Dispose()
来确认交易,从中触发这些事件是否有任何问题?还是在我的界面上有一个单独的 Confirm()
方法会更好,它除了用于清理临时数据的 Dispose()
之外还触发事件?我想不出任何我想确认但不想处理交易的情况。然而,我并不完全清楚在 Dispose()
中我应该做什么和不应该做什么。
public enum TransactionStatus
{
NotRun, // the Transaction has not been run, or has been undoed back to the original state
Successful, ///the action has been run and was successful
Error //there was an attempt to run the action but it failed
}
/// <summary>
/// Generic transaction interface
/// </summary>
public interface ITransaction
{
TransactionStatus Status { get; }
/// <summary>
/// Attempts the transaction returns true if successful, false if failed.
/// If failed it is expected that everything will be returned to the original state.
/// Does nothing if status is already Successful
/// </summary>
/// <returns></returns>
bool Go();
/// <summary>
/// Reverts the transaction
/// Only does something if status is successful.
/// Should return status to NotRun
/// </summary>
void Undo();
/// <summary>
/// A message describing the cause of the error if Status == Error
/// Otherwise equal String.Empty
/// </summary>
string ErrorMessage { get; }
}
最佳答案
Dispose 不是一种特殊的方法——它不像构造函数或终结器或其他任何东西——它只是一种有用的模式,用于通知对象消费者已使用完它。它没有理由不能引发事件。
关于c# - 从 Dispose() 触发事件是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3252404/
我在 dispose 和 finalize 之间犹豫不决。这是我的示例代码: public class Car:IDisposable { public string name
我有一个 Activity,我在其中创建并订阅 Single 类的多个实例(每个实例都在单独的后台线程中执行一些工作)。对于每个订阅,我将创建的 Disposable 实例添加到 CompositeD
我有一个服务、一个存储库和一个 DbContext。存储库拥有 DbContext,服务拥有存储库。 我的存储库应该实现 IDisposable 吗?如果是这样,我的服务是否也应该实现 IDispos
我有一个基于字符串列表处理文件流的应用程序,字符串可以是磁盘上的文件,也可以是 Zip 文件中的文件。为了清理代码,我想重构打开文件的过程。 我创建了一个返回文件内容 Stream 的方法,但是因为流
有没有一种方法可以从 Dispose 方法中取消处置,换句话说,在调用 A.Dispose() 时不处置 A。 更新我没有很好地解释,因为我想不出一个很好的方式来表达它。我拥有的是一个在单独线程中运行
如以下示例实现即 https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose有一
我开始用 FXCop 来处理我的一些程序集,并注意到一些有趣的事情,我正试图全神贯注。 "Types that declare disposable members should also imple
当我创建表单时,自动生成的代码不包含重写的 Dispose 方法。这是否意味着没有为表单中的所有控件调用 Dispose? 最佳答案 当您在表单上调用 Dispose 时,它会为其 Controls
在我的 WPF 应用程序代码中,我收到以下警告: CA1001 Types that own disposable fields should be disposable Implement IDis
每当我读到 Close() 和 Dispose() 时,我都会看到很多只使用 Using Block 的引用资料,但我还没有找到如何在 WebMatrix C# Razor 语法中使用 Using B
我是 c# 和 VSTO 的新手。 我有一个 Excel 插件,我在其中创建了一个功能区和一些可以执行各种操作的新按钮。 我在 base.Dispose 中收到 NullReferenceExcept
我在 FCL 代码中发现了一个不寻常的样本。 这是 System.IO.BinaryReader 中的方法: protected virtual void Dispose(bool dispo
我正在尝试遵循以下教程并将其从 C# 转换为 Vb.net http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef
我有一个 LINQ 对象,其中添加了一个附加方法。该类没有一次性属性或方法,但 FxCop 引发错误“拥有一次性字段的类型应该是一次性的”并引用该类。 到目前为止我已经减少了代码,但仍然收到错误: p
因此,默认的处理模式实现如下所示: class SomeClass : IDisposable { // Flag: Has Dispose already been called? bo
我在 WinForms 应用程序中使用 RxFramework。我正在尝试运行 Observable 异步并使用 CancellationDisposable 在用户单击按钮时取消操作。但它不工作!
有很多从 .NET 中的函数返回 IDisposable 对象数组或列表的示例。例如,Process.GetProcesses()。 如果我调用该方法,我是否有责任在遍历数组的所有成员时对它们进行 D
如果我用 C# 编写一个实现 IDisposable 的类,为什么我不能简单地实现 public void Dispose(){ ... } 处理释放任何非托管资源? 是 protected virt
LibGDX - 是否应该先执行 texture.dispose() 然后再执行 batch.dispose()?还是没有什么不同? 我有以下简单的代码。我的问题是 texture.dispose()
当应用程序收到来自服务的调用时,它会为每个调用打开一个表单。用户必须在每个窗口中进行输入并关闭它。为了让用户的工作顺利进行,我试图在显示下一个窗口时重新激活用户正在处理的窗口。 执行此操作的方法如下:
我是一名优秀的程序员,十分优秀!