我有一些C#代码要添加到现有的VB.net项目中。 C#类被设计为html解析器
最初使用过在线转换器并且能够让大部分课程正常工作,但下面的部分仍无法正常工作。不幸的是,我缺乏解决这个问题的知识。
我发布了整篇文章,但如果有人能澄清前两行,我认为这就足够了。 AttributeNameValuePair是一个包含属性的独立类。
进一步使用一些内联函数,也会欣赏它的一个例子。或者将这些作为单独的函数并且仅在内部留下引用会更容易吗?
事先感谢您的帮助。
private readonly Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>> commandsDictionary = new Dictionary<string, Action<DocumentModel, IEnumerable<AttributeNameValuePair>>>()
{
{ "b", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Bold = true) },
{ "i", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Italic = true) },
{ "u", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.UnderlineStyle = UnderlineType.Single) },
{ "strike", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Strikethrough = true) },
{ "sub", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Subscript = true) },
{ "sup", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => GetLastRun(doc).CharacterFormat.Superscript = true) },
{ "div", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) =>
{
foreach(var arg in args)
{
if(arg.AttributeName == "align")
{
HorizontalAlignment align;
switch(arg.AttributeValue)
{
case "center":
align = HorizontalAlignment.Center;
break;
case "right":
align = HorizontalAlignment.Right;
break;
case "justify":
align = HorizontalAlignment.Justify;
break;
default:
align = HorizontalAlignment.Left;
break;
}
}
}
})},
{ "br", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => doc.Sections[0].Blocks.Add(new Paragraph(doc))) },
{ "span", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) => {})},
{ "font", new Action<DocumentModel, IEnumerable<AttributeNameValuePair>>((doc, args) =>
{
foreach(AttributeNameValuePair arg in args)
{
int? size = null;
string fontName = null;
// Some dummy values.
if (arg.AttributeName == "size")
size = 10 + 3 * int.Parse(arg.AttributeValue);
else if (arg.AttributeName == "face")
fontName = arg.AttributeValue.Split(',').First();
var lastFormat = GetLastRun(doc).CharacterFormat;
if (size.HasValue)
lastFormat.Size = size.Value;
if (fontName != null)
lastFormat.FontName = fontName;
}
})},
};
答案 0 :(得分:0)
这是哪个版本的vb.net? Lambda表达式在每个表达式中的工作方式略有不同。多线lambda仅在VS2010 +中受支持。对于VS2008,您通常最终必须将匿名方法转换为实际方法并通过AddressOf
引用它们。我注意到,自动翻译在从C#转换匿名/ lambda类型表达式时通常似乎失败了。
在任何情况下,与C#相比,VB中的匿名方法语法有点笨拙且不同,令人困惑。对于单行方法,您可以执行类似的操作(为清晰起见,我使用简单类型):
Private cmdDictionary As New Dictionary(Of String, Action(Of Integer, String))
'adding items'
cmdDictionary.Add("div", New Action(Of Integer, String) _
(Sub(x As Integer, y As String) Console.WriteLine(y & ":" & CStr(x))))
'then to access the dictionary'
cmdDictionary.Item("div")(42, "foobar")
对于多线(在vs2010中),模式如下:
cmdDictionary.Add("div", New Action(Of Integer, String) _
(Sub(x As Integer, y As String)
x = 2 * x
Console.WriteLine(y & ":" & CStr(x))
End Sub))
答案 1 :(得分:0)
您可以将此C#代码1:1翻译为VB.Net。
在此示例中,我仅添加了b
和div
,但它应该足以引导您:
Private Readonly commandsDictionary As Dictionary(Of string, Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair))) =
new Dictionary(Of string, Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair)))() From
{
{ "b", new Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair)) (Sub(doc, args) GetLastRun(doc).CharacterFormat.Bold = true) },
{ "div", new Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair))(Sub(doc, args)
For Each arg in args
if arg.AttributeName = "align" Then
Dim align as HorizontalAlignment
Select arg.AttributeValue
case "center":
align = HorizontalAlignment.Center
case "right":
align = HorizontalAlignment.Right
case "justify":
align = HorizontalAlignment.Justify
case else:
align = HorizontalAlignment.Left
end select
end if
Next
End Sub)}}
}
您可能想为Action(Of DocumentModel, IEnumerable(Of AttributeNameValuePair))
创建一个类型别名,因为这会大大提高可读性。