我使用以下代码使用源对象创建特定目标类型的新实例,其中我将所有原始类型属性值从源对象复制到目标对象:
Function GetDestinationObjectFromSourceObject(pSourceObject As Object, pDestinationType As Type) As Object
Dim oDestinationObject = Activator.CreateInstance(pDestinationType)
For Each oPropertyDescriptor As PropertyDescriptor In TypeDescriptor.GetProperties(pSourceObject)
If TypeDescriptor.GetProperties(oDestinationObject).Contains(oPropertyDescriptor) Then
TypeDescriptor.GetProperties(oDestinationObject).Item(oPropertyDescriptor.Name).SetValue(oDestinationObject, oPropertyDescriptor.GetValue(pSourceObject))
End If
Next
Return oDestinationObject
End Function
现在。我有一个 List(Of Class1) 并希望使用相同的通用方法获取 List(Of Class2)。在这里,我想通过 List(Of Class1) 和目标类型(即 GetType(Class2)) 我怎样才能实现相同的目标?