2014-02-26 109 views
0

使用VB.NET,我有部份类将IEnumerable(Of Object)转换为实现IEnumerable(Of Object)的类?

Public Class MyCollectionClass 
    Implements IEnumerable(Of MyClass) 

    Public Property MadeThisClassCuzINeedToSetThis() As String 
    ' code here 
End Class 

我想这样做,但得到一个异常说我不能这样做演员。

Dim objColl As MyCollectionClass 
objColl = CType(IEnumerable(Of MyClass), MyCollectionClass) 

有谁能告诉我如何让这个工作。谢谢。

+0

你不觉得结束类正在被视为字符串? –

+1

你好,欢迎来到Stack Overflow。我想你也许应该删除C#标记 - 我在这里看不到任何C#! – Baldrick

+0

Doh! Thx,C#是自动建议的。看起来它已被删除。 – pseudoware

回答

1

请参阅此VB.NET/C# casting cheat sheetdocumentation on CType。主要的问题是第一个参数应该是要转换的实例,而不是它的类型。这应该工作:

Dim myEnumerable As IEnumerable(Of MyObjectClass) = New MyCollectionClass() 
Dim objColl = CType(myEnumerable, MyCollectionClass) 
' objColl's type is inferred As MyCollectionClass 

(注意,由于MyClass是一个关键词,我想你确实有一个不同的类名在那里,我就在我的例子改为MyObjectClass

+0

Thx,检查出来。是的,我的课不是MyClass :) – pseudoware

相关问题