.NET中的扩展通用方法(VB.NET)

ser*_*hio 0 .net vb.net generics extension-methods

我想在VB.NET中实现一个扩展方法,它将克隆类型为T的对象.

说,我想要

Dim cust1 as New Customer() //...
Dim cust2 as New Customer() //...

cust2 = cust1.Clone()

''
' My extension method '
''
<Runtime.CompilerServices.Extension()> _
Public Function Clone(Of T As {Class, New})(ByVal obj As T) As T 
  Dim objClone As New T
    ' clonning stuff '
    ' objClone = CType(GetAnObjClone(), T) '
  Return objClone
End Function

    Dim c As MyObject
    Dim cc As MyObject = c.Clone() ' does work!!! cool... '
Run Code Online (Sandbox Code Playgroud)

要删除的问题.

cdh*_*wie 5

克隆应该是对象本身作为常规方法执行的操作,而不是扩展方法.有关示例,请参阅有关MemberwiseCloneMSDN文档.