如果有人正在处理可空类型,请遵循 @Hans Passant 函数:
For Each field As FieldInfo In fields
' Extra check for nullable
If field.FieldType.AssemblyQualifiedName.Contains("System.Nullable") Then
' Insert proper type
If field.FieldType.AssemblyQualifiedName.Contains("System.DateTime") Then
table.Columns.Add(field.Name, Type.GetType("System.DateTime"))
End If
Else
table.Columns.Add(field.Name, field.FieldType)
End If
Next
价值观:
For Each item As T In list
Dim row As DataRow = table.NewRow()
For Each field As FieldInfo In fields
' Check if value is null
If field.GetValue(item) is nothing Then
Continue For
End If
row(field.Name) = field.GetValue(item)
Next
table.Rows.Add(row)
Next