投我有這樣的類:無法從基礎的XElement類
Public Class MyXElement
Inherits XElement
Public Sub New(other As XElement)
MyBase.New(other)
End Sub
Public Sub New(name As XName)
MyBase.New(name)
End Sub
Public Sub New(other As XStreamingElement)
MyBase.New(other)
End Sub
Public Sub New(name As XName, content As Object)
MyBase.New(name, content)
End Sub
Public Sub New(name As XName, ParamArray content() As Object)
MyBase.New(name, content)
End Sub
End Class
爲什麼下面的代碼會失敗?
Dim x1 As XElement = <demo></demo>
Dim x2 As MyXElement
x2 = x1
我得到異常:無法轉換類型「System.Xml.Linq.XElement」的對象鍵入「MyXElement」。
你沒有的隱式轉換操作符:http://stackoverflow.com/questions/1312549/is-there-a-way -to-define-an-implicit-conversion-operator-in-vb-net –
是的,但僅僅是因爲它不允許爲繼承類型定義轉換運算符。通常從基類到繼承的轉換不是問題,但在這種情況下不是問題:/ – Dima
*「是的,但僅限於不允許爲繼承類型定義轉換運算符」*。事實上,你是對的。 –