我读了很多关于这个主题的帖子; 其中以及最近的.NET - 将通用集合转换为数据表.不幸的是,一切都无济于事.
我有一个通用的结构集合:
Private Structure MyStruct Dim sState as String Dim lValue as Long Dim iLayer as Integer End Structure Dim LOStates As New List(Of MyStruct)
我需要用这个结构列表填充DataTable,但不知道如何去做.我在Visual Studio 2008中使用vb.net.
任何见解将不胜感激
您链接的代码假定成员被声明为属性.您没有声明属性.你可以使用Reflection:
Imports System.Reflection ... Public Shared Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable Dim table As New DataTable() Dim fields() As FieldInfo = GetType(T).GetFields() For Each field As FieldInfo In fields table.Columns.Add(field.Name, field.FieldType) Next For Each item As T In list Dim row As DataRow = table.NewRow() For Each field As FieldInfo In fields row(field.Name) = field.GetValue(item) Next table.Rows.Add(row) Next Return table End Function