感谢abmv的提示,这是我使用endedup的最终结果。我仍然需要编写一些特定属性的代码,但大多数属性都是通过这种机制自动处理的。
Private Sub Transfer(ByVal src As Object, ByVal dst As Object)
Dim theSourceProperties() As Reflection.PropertyInfo
theSourceProperties = src.GetType.GetProperties(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
For Each s As Reflection.PropertyInfo In theSourceProperties
If s.CanRead AndAlso (Not s.PropertyType.IsGenericType) And (s.PropertyType.IsPrimitive Or s.PropertyType.UnderlyingSystemType Is GetType(String)) Then
Dim d As Reflection.PropertyInfo
d = dst.GetType.GetProperty(s.Name, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
If d IsNot Nothing AndAlso d.CanWrite Then
d.SetValue(dst, s.GetValue(src, Nothing), Nothing)
End If
End If
Next
End Sub