【发布时间】:2012-02-02 11:25:29
【问题描述】:
我得到了这个在 Visual Studio 2010 中完美运行的 C# sn-p:
使用系统; 使用 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 不是“字符串”的成员。这是阻止我的解决方案工作的唯一原因!
我在 MSDN 上进行了研究,它说错误意味着它必须被模块包围,但不是编码器,我不知道如何修复它。 sn-p 是为了帮助我的 ?lang=en-GB 查询字符串出现在我的多语言网站中。任何帮助将不胜感激!
更新:我有 ApplyCulturetoUrl 的工作,只需单击 Visual Studio 中的很多图标,它就会吐出这个:
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() 用波浪形的蓝色下划线表示扩展方法只能在模块中定义。我正在使用您的所有建议!感谢您的坚持!
【问题讨论】:
-
我感觉这将是一种扩展方法,在另一个项目的其他地方定义。在 C# 解决方案中,右键单击并选择“转到定义”,这应该可以帮助您找到它。
-
嘿,失望,我认为你是对的!当我去定义时,有很多选择,我不知道该选择哪一个!不过谢谢!
标签: c# vb.net extension-methods c#-to-vb.net