.NET 扩展事件

发布于 2024-08-04 14:24:35 字数 631 浏览 8 评论 0原文

我想将事件添加到一些内置的 .NET 类中。例如,我想将以下事件添加到通用 List(T) 中:

  • ItemAdded
  • ItemRemoved
  • ListChanged

我可以想到几种方法来实现此目的,但我不确定哪种方法最好。

  1. 我可以使用“扩展事件”,也就是说,如果有这样的东西或方法可以使用扩展方法来模拟类似事件的功能。
  2. 我可以继承List(T)覆盖必要的方法和属性。
  3. 我可以从 List(T)shadow 继承必要的方法和属性。
  4. 我可以创建一个实现 IList(T) 的新类,然后包装 List(T) 的剩余功能,即 IList(T) 是丢失的。

显然,选项 4 的工作量最大,如果我只想延长一门课程,这没什么大不了的。然而,我也想避免令人讨厌的副作用。

每种方法的优点、缺点和注意事项是什么?

注意:我在这里并不真正关心 IList(T)。我发现自己也想向其他类添加“扩展事件”。

I want to add events to some built in .NET classes. For example I want to add the following events to a generic List(T):

  • ItemAdded
  • ItemRemoved
  • ListChanged
  • etc

I can think of a few ways to accomplish this, but I'm not sure which approach is best.

  1. I could use "extension events", that is, if there is such a thing or a way to use extension methods to simulate event-like functionality.
  2. I could inherit from List(T) and override the necessary methods and properties.
  3. I could inherit from List(T) and shadow the necessary methods and properties.
  4. I could create a new class that implements IList(T) and then wrap the remaining functionality of List(T) that IList(T) is missing.

Obviously option 4 is the most work, which is not a big deal if I only wanted to extend one class. However, I also want to avoid nasty side effects.

What are the advantages, disadvantages, and caveats for each approach?

Note: I'm not really concerned here with IList(T) specifically. I find myself wanting to add "extenstion events" to other classes as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-08-11 14:24:35
  1. 不存在“扩展事件”这样的东西,并且您不能使用扩展方法来模拟它:扩展事件无法向现有类添加状态,而这正是您所需要的。

  2. 我认为你的意思是覆盖而不是重载。这会起作用,但这有点笨拙 - 并且意味着您无法使用 LINQ 等中的正常 ToList ,因为那样您只会得到一个 List

  3. 影子比凌驾更令人讨厌。这意味着事件可能会也可能不会引发,具体取决于调用者调用该方法的方式。恶心。

  4. 这会更好,您可以向 List 添加扩展方法来包装它。一个缺点是,如果其他人“知道”解包的集合,您将无法发现以这种方式所做的更改。

您是否查看过 ObservableCollection?请注意,如果您在构造函数中提供列表,则这将从列表中复制,而不是包装列表。

  1. There's no such thing as "extension events" and you can't use extension methods to simulate this: extension events can't add state to existing classes, which is what you'd require.

  2. I think you mean override rather than overload. This will work, this it's slightly clumsy - and means you couldn't use the normal ToList from LINQ etc, because then you'd just get a List<T>.

  3. Shadowing is nastier than overriding. It means that the events may or may not be raised based on how the caller is calling the method. Ick.

  4. This would be preferable, and you could add an extension method to List<T> to wrap it. One disadvantage is that if anyone else "knows about" the unwrapped collection, you won't spot changes made that way.

Have you looked at ObservableCollection<T>? Note that this will copy from a list if you provide it in the constructor, rather than wrapping the list.

任性一次 2024-08-11 14:24:35

“扩展事件”不存在,扩展属性或除扩展方法之外的任何内容也不存在。就我个人而言,我希望看到扩展属性(假设它们只是函数的包装器),但我不确定如何以扩展所需的静态方式实现扩展事件。

没有什么可以阻止您将这些事件添加到类本身中。但是,查看 BindingList 可能会更成功,因为它更符合您想要执行的监视活动。

"extension events" don't exist, nor do extension properties or anything other than extension methods. Personally I'd like to see extension properties (given that they're just wrappers for functions), but I'm not sure how you could implement extension events in the static fashion that extensions require.

There is nothing stopping you from adding those events to the class itself. However, you might have more success looking at BindingList<T>, since it's more in line with the monitoring activity you appear to want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文