将Handles(HttpApplication)方法从VB.NET迁移到C#?

时间:2013-02-16 23:29:54

标签: c# vb.net events handles

我正在将一些代码从VB.NET迁移到C#(4.0)。

我发现结构如:

Private Sub WhitePointHttpApplicationBase_BeginRequest(sender As Object, e As System.EventArgs) Handles Me.BeginRequest

End Sub

在C#中翻译此类行为最直接的是什么?

1 个答案:

答案 0 :(得分:0)

在构造函数中添加this.BeginRequest+=WhitePointHttpApplicationBase_BeginRequest;

您还需要存在的方法: private void WhitePointHttpApplicationBase_BeginRequest(sender As Object, e As System.EventArgs) { //Your event code here }

以下是您带有更正的评论中的代码:

namespace WhitePoint.Solutions.Web 
{ 
    public abstract class WhitePointHttpApplicationBase : HttpApplication { 

        protected WhitePointHttpApplicationBase()
        {
            this.BeginRequest += WhitePointHttpApplicationBase_BeginRequest; 
        }
        #region "Member" 
        #endregion 

        private void WhitePointHttpApplicationBase_BeginRequest(object sender, EventArgs e) { } 
    } 
} 

this.BeginRequest +=不在构造函数中。

抽象类现在如何默认保护构造函数,如果您希望代码运行,任何继承的类都应该调用此基础构造函数。