C# 代码转换为 vb.net代码

JERRY_LIU 2015-02-05 03:41:47
各位高手,我这里有一段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;
}
}

...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcmakebest 2015-02-06
  • 打赏
  • 举报
回复
这个应该行的.
    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
mjzxlmg 2015-02-06
  • 打赏
  • 举报
回复
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
JERRY_LIU 2015-02-06
  • 打赏
  • 举报
回复
谢谢楼上各位,我先试 下
TianShangYouHuiJi 2015-02-06
  • 打赏
  • 举报
回复
。。。vb可以直接调用c#的dll的。干嘛还要转换,直接用吧
threenewbee 2015-02-05
  • 打赏
  • 举报
回复
很简单,直接编译你的程序,用ILSpy打开,下拉选择VB,就能自动转换。
  • 打赏
  • 举报
回复
比如说,你只要从 datatable 中将字段a是“vb”字符打头、并且c>25的记录转换为集合,就可以写
Dim result = (From x In datatable.Rows
                Let a = CType(x("column a"), String)
                Let c = CType(x("column c"), Integer)
                Where a.StartsWith("vb") And c > 25
                Select New MyType With
                    {
                        .field_a = a,
                        .field_b = x("column b"),
                        .field_c = x("column c")
                    }).ToList
这样,你可以看到变量 result 就是一个 List(Of MyType) 的集合。 应该学会高效率的 Linq 编程知识,它有更多更广的应用。
  • 打赏
  • 举报
回复
反射不但非常慢,而且难易调试 --> 反射不但非常慢,而且难以调试 要写出强类型的代码。该用接口的地方,不要用反射。该用Linq的地方,不要用反射.......只有极个别地方才需要反射。
  • 打赏
  • 举报
回复
尽量不要滥用反射。反射不但非常慢,而且难易调试。真正善用反射的人不会滥用反射的,只在该用的地方去用。 要从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
记住,不需要反射!
wind_cloud2011 2015-02-05
  • 打赏
  • 举报
回复
'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
wind_cloud2011 2015-02-05
  • 打赏
  • 举报
回复
http://www.dotnetspider.com/convert/Csharp-To-Vb.aspx
Liekkas 2015-02-05
  • 打赏
  • 举报
回复
从数据库里读取配置,然后创建对象。不好意思,VB我不熟

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧