各位高手,我这里有一段C#的代码,是将datatable 转换为泛型数据的,因为C#不是很熟,泛型也用的不多,不怎么看的懂,请看哪位可以帮忙将这堆代码转为vb.net,不胜感谢!
public class ModelHelper
{
public static List<T> DataTableToEntityList<T>(DataTable dt)
{
List<T> entiyList = new List<T>();
Type entityType = typeof(T);
PropertyInfo[] entityProperties = entityType.GetProperties();
int index = 0;
string str = "";
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
foreach (PropertyInfo propInfo in entityProperties)
{
if (dt.Columns.Contains(propInfo.Name))
{
if (!row.IsNull(propInfo.Name))
{
str = propInfo.Name;
try
{
propInfo.SetValue(entity, row[propInfo.Name], null);
}
catch (Exception e)
{
throw new Exception(propInfo.Name + ":" + e.ToString());
}
}
}
}
entiyList.Add(entity);
index++;
}
return entiyList;
}
}
------解决思路----------------------
http://www.dotnetspider.com/convert/Csharp-To-Vb.aspx
------解决思路----------------------
'Public Class ModelHelper
Public Shared Function DataTableToEntityList(Of T)(ByVal dt As DataTable) As List(Of T)
Dim entiyList As List(Of T) = New List(Of T)()
Dim entityType As Type = Type.GetType("System.Int32")
Dim entityProperties() As PropertyInfo = entityType.GetProperties()
Dim index As Integer = 0
Dim str As String = ""
Dim row As DataRow
For Each row In dt.Rows
Dim entity As T = Activator.CreateInstance(Of T)()
Dim propInfo As PropertyInfo
For Each propInfo In entityProperties
If dt.Columns.Contains(propInfo.Name) Then
If Not row.IsNull(propInfo.Name) Then
str = propInfo.Name
Try
propInfo.SetValue(entity, row(propInfo.Name), Nothing)
Catch e As Exception
Throw New Exception(propInfo.Name + ":" + e.ToString())
End Try
End If
End If
Next
entiyList.Add(entity)
index = index + 1
Next
Return entiyList
End Function
'End Class
------解决思路----------------------
尽量不要滥用反射。反射不但非常慢,而且难易调试。真正善用反射的人不会滥用反射的,只在该用的地方去用。
要从datatabl转换到强类型的集合,很容易,随时可以写出强类型编译代码。
假设你的数据实体的类型定义为
Class MyType
Property field_a As String
Property field_b As Integer
Property field_c As DateTime
End Class
要从一个datatable转换,只要这样写就可以了
Dim result As List(Of MyType) = (From x In datatable.Rows
Select New MyType With
{
.field_a = x("column a"),
.field_b = x("column b"),
.field_c = x("column c")
}).ToList
记住,不需要反射!
------解决思路----------------------
Public Class ModelHelper
Public Shared Function DataTableToEntityList(Of T)(ByVal dt As DataTable) As List(Of T)
Dim entiyList As New List(Of T)()
Dim entityType As Type = GetType(T)
Dim entityProperties() As Reflection.PropertyInfo = entityType.GetProperties()
Dim index As Integer = 0
Dim str As String = ""
For Each row As DataRow In dt.Rows
Dim entity As T = Activator.CreateInstance(Of T)()
For Each propInfo As Reflection.PropertyInfo In entityProperties
If dt.Columns.Contains(propInfo.Name) Then
If Not row.IsNull(propInfo.Name) Then
str = propInfo.Name
Try
propInfo.SetValue(entity, row(propInfo.Name), Nothing)
Catch e As Exception
Throw New Exception(propInfo.Name & ":" & e.ToString())
End Try
End If
End If
Next propInfo
entiyList.Add(entity)
index += 1
Next row
Return entiyList
End Function
End Class
------解决思路----------------------
这个应该行的.
Public Shared Function DataTableToEntityList(Of T)(ByVal dt As DataTable) As List(Of T)
Dim entiyList As New List(Of T)
Dim entityProperties As PropertyInfo() = GetType(T).GetProperties
Dim index As Integer = 0
Dim str As String = ""
Dim row As DataRow
For Each row In dt.Rows
Dim entity As T = Activator.CreateInstance(Of T)()
Dim propInfo As PropertyInfo
For Each propInfo In entityProperties
If (dt.Columns.Contains(propInfo.Name) AndAlso Not row.IsNull(propInfo.Name)) Then
str = propInfo.Name
Try
propInfo.SetValue(entity, row.Item(propInfo.Name), Nothing)
Catch e As Exception
Throw New Exception((propInfo.Name & ":" & e.ToString))
End Try
End If
Next
entiyList.Add(entity)
index += 1
Next
Return entiyList
End Function