【发布时间】:2015-03-04 11:47:43
【问题描述】:
我知道我可以在 C# 中使用以下符号显式转换类型:(Type)Object。
我正在将 Visual Basic 代码翻译成 C#。
VB代码:
TempTrans(j) = CType(FillTranslator.IndxLanguage.Item(j), Translator.IndxLangauges.IndxTranslation).Translations.Item(Row.Name.Trim) //This is the line I need help with!
这是一个结构体(来自 Translator 类)
Structure IndxLangauges
Public IndxLanguage As Collection
Structure IndxTranslation
Public Language As Integer
Public Name As String
Public Translations As Collection
End Structure
End Structure
还有:
Private Shared FillTranslator As Translator.IndxLangauges
在 C# 中我有:
public struct IndxLanguages
{
public System.Collections.Generic.List<string> IndxLanguage;
public struct IndxTranslation
{
public int Language;
public string Name;
public System.Collections.Generic.List<string> Translations;
};
};
private static Translator.IndxLanguages FillTranslator;
TempTrans[j] = ((Translator.IndxLanguages.IndxTranslation)FillTranslator.IndxLanguage[j]).Translations[Row.TypeName.Trim]; //Error here
我收到错误:无法将类型字符串转换为 Translator.IndxLanguages.IndxTranslation
我不明白转换后的代码中发生了什么(在 VB 中):.Translations.Item(Row.Name.Trim)。
谁能帮我理解 VB 中的CType 代码,尤其是后面的点符号?
【问题讨论】:
-
可能重复(尽管它似乎不是规范的(10 票,从 2012 年开始)):How do I translate VB.NET's CType() to C#?
-
某处必须有一个规范问题(搜索引擎在其结果中包含的标题中的一般问题)。这一定是在 Stack Overflow 存在的最初几天或几周内被问到的。有人能找到吗? 2008 年或 2009 年必须有一个。这是 2010 年的,但只有 8 票:What is the C# equivalent of CType in VB.NET?
-
更规范(50 票,自 2010 年起):C#'s equivalent to VB.NET's DirectCast?