如何修复从C#翻译的这段简短的VB片段?

时间:2012-02-02 11:25:29

标签: c# vb.net extension-methods c#-to-vb.net

我获得了这个在Visual Studio 2010中完美运行的C#片段:

    使用系统;     使用System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
    LinkButton senderLink = (LinkButton)sender;
    Response.Redirect(Request.RawUrl.ApplyCultureToUrl(senderLink.CommandArgument));
}
}

每个在线转换器(和SharpDevelop)都将其转换为VB:

Imports System
Imports System.Web.UI.WebControls

Public Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub

Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
Dim senderLink As LinkButton = DirectCast(sender, LinkButton)

Response.Redirect(Request.RawUrl.ApplyCultureToUrl(senderLink.CommandArgument))
End Sub
End Class

VS也是这样做的:

Public NotInheritable Class Localization
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Function ApplyCultureToUrl(rawUrl As String, culture As String) As String
Dim modifiedUrl As String

System.Runtime.CompilerServices.Extension 只能在模块中运行。

问题:

在Visual Studio 2010中, Request.RawUrl.ApplyCultureToUrl 以蓝色下划线,并且不提供解决方案。它只是说 ApplyCultureToUrl不是'String'的成员。这是阻止我的解决方案工作的唯一因素!

我研究过MSDN,它说错误意味着它必须被模块包围,但不是编码器,我不知道如何解决它。该片段是为了帮助我的多语言网站显示我的?lang = en-GB查询字符串。真诚地感谢任何帮助!


更新:我只是通过点击Visual Studio中的很多图标来使ApplyCulturetoUrl工作,然后将其吐出:

Partial Public Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub

Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
    Dim senderLink As LinkButton = DirectCast(sender, LinkButton)
    Response.Redirect(ApplyCultureToUrl(Request.RawUrl, senderLink.CommandArgument))
End Sub

Private Sub body()
    Throw New NotImplementedException
End Sub

Private Function ApplyCultureToUrl(p1 As String, p2 As String) As String
    Throw New NotImplementedException
End Function
End Class

这并没有给我任何错误,虽然我不知道它是否正确......呵呵......现在,代码中唯一给我错误的部分是本地化.vb,一个类,就像这样:

Public NotInheritable Class Localization
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Shared Function ApplyCultureToUrl(rawUrl As String, culture As String) As String
 Dim modifiedUrl As String

并且System.Runtime.CompilerServices.Extension()以波浪形蓝色加下划线,并且它表示扩展方法只能在模块中定义。我正在使用你的所有建议!谢谢你坚持下去!

2 个答案:

答案 0 :(得分:1)

将光标移至C#文件中的ApplyCultureToUrl关键字,然后按F12按钮。此方法在某处定义为扩展方法。

我不知道Visual Basic是否支持扩展方法,如果不支持,可以在本地VB中定义它:

Private Function ApplyCultureToUrl(RawUrl as String, Command as String) As String
   // body
End Function

在这种情况下,您将按如下方式调用它:

Response.Redirect(ApplyCultureToUrl(Request.RawUrl, senderLink.CommandArgument))

答案 1 :(得分:1)

看起来ApplyCultureToUrl是一种扩展方法。

解决这个问题就像导入定义ApplyCultureToUrl的命名空间一样简单。

然而,它可能比这更复杂:

在VB.Net中,扩展方法必须在一个模块中,这必须是公开的,以引用其他项目的扩展。要包含它们,您只需要引用包含的程序集并导入正确的命名空间。

VB.Net扩展方法的MSDN页面为here