2016-07-19 20 views
-2

我想做一些類似於這些的東西,儘管如此,但我可以把它放在我的基類中,並完成它?返回我的新類型? vb.net

returnVal = new TypeOf(Me) 
    returnVal = new Me.GetType() 
    returnVal = new Me 
    returnVal = new TypeOf(Me) 
    returnVal = Me.new() 
    returnVal = New class(Of me) 
    returnVal = New me.class 
    returnVal = me.class.new() 
    returnVal = new typeof me.class 
+0

你沒嘗試所有可能的排列,它是'returnVal = Me.GetType()'你不能,如果你不寫代碼vb.net理解'New'關鍵字。不理解「Overridable」或「Implements」關鍵字已經足夠普遍,但這就是您真正應該使用的。 –

+0

那麼,你想創建一個與當前實例相同類型的新實例嗎?你在這裏沒有任何意義,因爲如果你打算使用'New'關鍵字來調用構造函數,那麼你必須明確指定一個數據類型。 Type類的一個實例不是數據類型。它是一個表示數據類型的對象。 – jmcilhinney

+0

問題是,你爲什麼要編寫這樣的代碼呢?它是否在基類中,並且您希望派生類也能夠使用它?如果沒有,那麼你已經編寫了當前類的代碼,爲什麼不明確指定類型? – jmcilhinney

回答

0

我做到了

Class ClassToClone 
    Implements ICloneable 

    Public Overridable Function Clone() As Object Implements ICloneable.Clone 
     Return CloneVal({}) 
    End Function 

    Public Function CloneVal(args() as Object) As Object 
     Dim t As Type = MyClass.GetType() 

     Dim constr As Reflection.ConstructorInfo = t.GetConstructor(New Type() {}) 

     Dim result As Object = constr.Invoke(args) 

     Return result 
    End Function 
End Class