vb.net字典(字符串,对象)enum.getName()



我正在执行vb.net winform应用程序。这是c#。

的迁移

在C#中有一个这样定义的变量。

 private static Dictionary<string, ExportFormatType> dicExtensiones =
            new Dictionary<string, ExportFormatType> {
                        {".pdf", ExportFormatType.PortableDocFormat},
                        {".doc", ExportFormatType.WordForWindows},
                        {".xls", ExportFormatType.Excel},
                        {".rtf", ExportFormatType.RichText},
                        {".html", ExportFormatType.HTML40},
                        {".txt", ExportFormatType.Text}
                   };

我迁移到了。

   Private Shared dicExtensiones = New Dictionary(Of String, ExportFormatType) From
                            {{".pdf", ExportFormatType.PortableDocFormat},
                             {".doc", ExportFormatType.WordForWindows},
                             {".xls", ExportFormatType.Excel},
                             {".rtf", ExportFormatType.RichText},
                             {".html", ExportFormatType.HTML40},
                             {".txt", ExportFormatType.Text}}

现在,我需要循环遍历所有iTems并获得每个值...

在C#中就是这样。

 List<String> lista = new List<string>();
            foreach (var item in dicExtensiones)
            {
                lista.Add(Enum.GetName(typeof(ExportFormatType), item.Value));
                lista.Add("*" + item.Key);
            }

我遇到的问题是我确实知道如何迁移

Enum.GetName(typeof(ExportFormatType), item.Value);

to vb.net,因为 Enum.GetName不存在vb.net

我该怎么做?

它可以像vb

中的下面一样
Dim lista As List(Of [String]) = New List(Of String)()
For Each item As var In dicExtensiones
    lista.Add([Enum].GetName(GetType(ExportFormatType), item.Value))
    lista.Add("*" + item.Key)
Next

在VB中,Enum是一个关键字,也是类名称,因此您需要在代码中逃脱它。逃脱的语法类似于SQL:

[Enum].GetName

通过逃脱,您正在告诉编译器您是指使用该名称而不是关键字的标识符。例如,您可能还需要偶尔逃脱自己的类或变量名称:

Dim [property] As String = "belt, wallet with $50, casio watch"

Public Class [Class]
    Public Property Teacher As String
    Public Property Students As List(Of Student)
End Class

不过,在大多数情况下,最好只考虑使用其他名称来避免它。

最新更新