gpt4 book ai didi

c# - 从C#转换为VB的问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:40 25 4
gpt4 key购买 nike

我正在尝试将此代码从C#转换为VB。尝试使用第三方工具,但未成功。有人可以帮我吗。谢谢

private static string RemoveInvalidHtmlTags(this string text)
{
return HtmlTagExpression.Replace(text, new MatchEvaluator((Match m) =>
{
if (!ValidHtmlTags.ContainsKey(m.Groups["tag"].Value))
return String.Empty;

string generatedTag = String.Empty;

System.Text.RegularExpressions.Group tagStart = m.Groups["tag_start"];
System.Text.RegularExpressions.Group tagEnd = m.Groups["tag_end"];
System.Text.RegularExpressions.Group tag = m.Groups["tag"];
System.Text.RegularExpressions.Group tagAttributes = m.Groups["attr"];

generatedTag += (tagStart.Success ? tagStart.Value : "<");
generatedTag += tag.Value;

foreach (Capture attr in tagAttributes.Captures)
{
int indexOfEquals = attr.Value.IndexOf('=');

// don't proceed any futurer if there is no equal sign or just an equal sign
if (indexOfEquals < 1)
continue;

string attrName = attr.Value.Substring(0, indexOfEquals);

// check to see if the attribute name is allowed and write attribute if it is
if (ValidHtmlTags[tag.Value].Contains(attrName))
generatedTag += " " + attr.Value;
}

// add nofollow to all hyperlinks
//if (tagStart.Success && tagStart.Value == "<" && tag.Value.Equals("a", StringComparison.OrdinalIgnoreCase))
// generatedTag += " rel=\"nofollow\"";

if (tag.Value.ToString() == "object")
{
generatedTag += (tagEnd.Success ? " height=\"374\" width=\"416\"" + tagEnd.Value : ">");
}
else
{
generatedTag += (tagEnd.Success ? tagEnd.Value : ">");
}


return generatedTag;
}));
}

最佳答案

转换此代码的问题是您有一个带有多行语句主体的lambda表达式:

(Match m) =>
{
...a lot of code
}


由于VB9不支持此功能,因此您需要将方括号中的代码放入其自己的函数中:

Private Function GetValue(m As Match) As String
....a lot of code
End Function


然后,您的RemoveInvalidHtmlTags代码将如下所示:

Return HtmlTagExpression.Replace(text, new MatchEvaluator(AddressOf GetValue))


您可以使用免费工具来翻译其余代码。

关于c# - 从C#转换为VB的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1981740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com