2012-11-27 53 views
2

請指導我如何獲得一個泛型類型參數的類型,在波紋管的情況:通用反序列化

Public Function DerializeFromFile(Of T)(ByVal fileName As String) As T 
    Dim serializer As New XmlSerializer(T.GetType()) '--------------- HELP !!!??? 
    Dim fileStream = New FileStream(fileName, 
        FileMode.Open, FileAccess.Read, FileShare.Read) 
    Dim loadedObject As T = DirectCast(serializer.Deserialize(fileStream), T) 
    Return loadedObject 
    End Function 
+1

它是C#中的typeof(T)',在VB中也必須有類似的語法。也許'TypeOf(T)'? –

+0

在VB中不是typeof,可能是GetType ... – serhio

+0

vb.net的類型:http://msdn.microsoft.com/en-us/library/0ec5kw18.aspx – Ric

回答

1

將其更改爲:

Dim serializer As New XmlSerializer(GetType(T)) 

在VB中,GetType運算符返回Type對於給定類型的對象。