mvc preview 2中,找到的一段表单数据填充实体的函数

Public Shared Sub UpdateFrom(ByVal value As Object, ByVal values As NameValueCollection, ByVal objectPrefix As String)
        Dim type As Type = value.GetType
        Dim name As String = type.Name
        Dim properties As PropertyInfo() = type.GetProperties
        Dim info As PropertyInfo
        For Each info In properties
            Dim str2 As String = info.Name
            If String.IsNullOrEmpty(objectPrefix) = False Then
                str2 = (objectPrefix & str2)
            End If
            If (values.Item(str2) Is Nothing) Then
                str2 = (name & "." & info.Name)
            End If
            If (values.Item(str2) Is Nothing) Then
                str2 = (name & "_" & info.Name)
            End If
            If (Not values.Item(str2) Is Nothing) Then
                Dim converter As TypeConverter = TypeDescriptor.GetConverter(info.PropertyType)
                Dim obj2 As Object = values.Item(str2)
                If Not converter.CanConvertFrom(GetType(String)) Then
                    Throw New FormatException(("No type converter available for type: " & info.PropertyType.ToString))
                End If
                obj2 = converter.ConvertFrom(values.Item(str2))
                info.SetValue(value, obj2, Nothing)
            End If
        Next
    End Sub

posted @ 2008-05-28 00:41  没剑  阅读(643)  评论(2编辑  收藏  举报