Vb.net 返回新类型的我?运动模拟
我想做一些类似于这些的事情,尽管这很有效, 所以我可以把它放在我的基类中,然后就可以用它了Vb.net 返回新类型的我?运动模拟,vb.net,types,constructor,return,Vb.net,Types,Constructor,Return,我想做一些类似于这些的事情,尽管这很有效, 所以我可以把它放在我的基类中,然后就可以用它了 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 = 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
我做到了
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
您没有尝试所有可能的排列,它是
returnVal=Me.GetType()
如果您不理解New
关键字,则无法编写vb.net代码。不理解Overridable
或Implements
关键字很常见,但这正是您真正应该使用的。因此,您想创建一个与当前实例类型相同的新实例吗?在这种情况下,所有这些都没有任何意义,因为如果要使用New
关键字调用构造函数,则必须显式指定数据类型。类型
类的实例不是数据类型。它是一个表示数据类型的对象。问题是,为什么要编写这样的代码?它是否在基类中,并且您希望派生类也能够使用它?如果没有,那么您已经为当前类编写了代码,那么为什么不显式地指定类型呢?