简体   繁体   English

对带有事件处理程序的VB / C#转换感到困惑

[英]Confused about VB/C# conversion with event handlers

I've used just about every conversion tool out there to convert a snippet of VB.NET to C#. 我几乎使用了每种转换工具将VB.NET的片段转换为C#。 Aside from some differences in conversion, the one thing that I am having trouble grasping is event handler conversion. 除了转换方面的一些差异外,我难以掌握的一件事是事件处理程序转换。 Looking at some pure C# code, I see things like this: 看一些纯C#代码,我看到这样的事情:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    ...
}

which looking at MSDN , raises the PreRender event. 看着MSDN ,引发了PreRender事件。 OK. 好。 Now, taking some VB.NET code: 现在,获取一些VB.NET代码:

Private Sub Page_PreRender(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Me.PreRender
....
EndSub

which handles the same PreRender event and translating it with a conversion tool ( DeveloperFusion seemed to give the cleanest conversion of the Event Handler) gave this result: 它处理相同的PreRender事件并使用转换工具对其进行转换( DeveloperFusion似乎提供了事件处理程序的最干净的转换),结果如下:

public class MyClass : UserControl
{
    private void Page_PreRender(object sender, System.EventArgs e)
    {
        ...
    }

    public MyClass()
    {
        PreRender += Page_PreRender;
    }
}

Being new to this, my main question is whether or not the original (or pure) C# code first listed above is basically the same as the converted C# code (other than maybe with the object sender portion)? 对此,我的主要问题是上面首先列出的原始(或纯净)C#代码是否与转换后的C#代码基本相同(除了object sender部分以外)? And if not a brief explanation would be much appreciated. 如果不是这样,简要的解释将不胜感激。

Essentially, I am just wondering whether the PreRender += Page_PreRender; 本质上,我只是想知道PreRender += Page_PreRender; portion is superfluous. 部分是多余的。

The first C# code block presentes an override of the base classes' OnPreRender function, while the second is an event handler to the PreRender event. 第一个C#代码块呈现了基类的OnPreRender函数的重写 ,而第二个是PreRender事件的事件处理程序。

Yes, they are different, but functionally they are more-or-less equivialent, because as you mentioned the base classes OnPreRender function will fire the PreRender event. 是的,它们是不同的,但是在功能上它们几乎是等价的,因为正如您提到的,基类OnPreRender函数将PreRender事件。

Essentially it represents two ways to accomplish the same thing, and the mechanism you choose is mostly a question of semantics. 本质上,它代表完成同一件事的两种方式,而您选择的机制主要是语义问题。 I say mostly because there may exist explicit reasons to choose one method over the other, but if you aren't aware of any, just choose one. 我说的主要是因为可能存在明显的理由选择一种方法而不是另一种方法,但是如果您不了解任何一种方法,则只需选择一种即可。

I prefer the override myself, as you typically use events to broadcast notifications to external listeners rather than to derived instances of an object. 我更喜欢覆盖我自己,因为您通常使用事件将通知广播到外部侦听器,而不是对象的派生实例。

The PreRender += Page_PreRender is not superfluous in your example, as it is required to hookup the event handler when you are using that mechanism. 在您的示例中, PreRender += Page_PreRender并不是多余的,因为在使用该机制时需要连接事件处理程序。 The VB version you posted is implemented using events rather that an override, so the conversion is correct. 您发布的VB版本是使用事件而不是替代实现的,因此转换是正确的。 The "wire-up" for these events are implicit in VB.net, so you won't find an equivalent statement in the VB version. 这些事件的“连接”在VB.net中是隐式的,因此在VB版本中找不到等效的语句。

Is the PreRender += Page_PreRender; PreRender += Page_PreRender; portion superfluous? 多余的部分?

No - that code is C#'s way of assigning the event handler, which is done in VB with the Handles Me.PreRender clause. 否-该代码是C#分配事件处理程序的方式,这是在VB中使用Handles Me.PreRender子句完成的。

Being a n00b, my main question is whether or not the original (or pure) C# code first listed above is basically the same as the converted C# code. 作为n00b,我的主要问题是上面首先列出的原始(或纯净)C#代码是否与转换后的C#代码基本相同。

Yes, the conversion tool is right ! 是的,转换工具是正确的


Explanation 说明

VB.NET's Handles is the equivalent to PreRender += Page_PreRender; VB.NET的Handles等效于PreRender += Page_PreRender; .

Handles basically does the exact same thing in the background; Handles基本上在后台执行完全相同的操作; adding an eventhandler. 添加一个事件处理程序。

So Codesnippet #1 and Codesnippet #2 are the same, except that you need to manage the handler by yourself in #2 (Like removing the handler if the Windows Form gets disposed). 因此, Codesnippet#1Codesnippet#2相同,除了您需要在#2中自己管理处理程序(就像处置Windows窗体一样删除处理程序)。

Codesnippet #1 (VB.NET) 代码片段1(VB.NET)

Private Sub Page_PreRender(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Me.PreRender
....
End Sub

Codesnippet #2 (VB.NET) 代码片段2(VB.NET)

AddHandler Me.PreRender, AddressOf Page_PreRender

So obviously, no converted code is superfluous. 因此很明显,没有任何转换后的代码是多余的。

The conversion was done correctly, but it is not equivalent to the original C#: 转换已正确完成,但等同于原始C#:

which looking at MSDN, raises the PreRender event. 在MSDN上,它引发了PreRender事件。

The original code is not a handler, it's an invoker. 原始代码不是处理程序,而是调用程序。 OnPreRender is called to raise PreRender . 调用OnPreRender引发PreRender If the VB code raised PreRender in Page_PreRender , it would lead to infinite recursion. 如果VB代码在Page_PreRender提高了PreRender ,则将导致无限递归。

The overridable "On[event name]" pattern is used to allow subclasses to "handle" events, but it's not the same as a handler. 可覆盖的“ On [事件名称]”模式用于允许子类“处理”事件,但它与处理程序不同。 The call to base / MyBase is what actually raises the event, so essentially by overriding one of these methods you're forcing an action to occur completely before or after (depending on where you call the base) all event handlers are invoked. base / MyBase的调用实际上是引发该事件的方法,因此本质上通过覆盖这些方法之一,您可以强制在调用所有事件处理程序之前或之后(取决于您调用base的位置)完全执行某项操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM