zoukankan      html  css  js  c++  java
  • 实体互转

    public class ConvertHelper
    {
    public static List<T2> ConvertToList<T1, T2>(List<T1> source)
    {
    List<T2> t2List = new List<T2>();
    if (source == null || source.Count == 0)
    return t2List;

    T2 model = default(T2);
    PropertyInfo[] pi = typeof(T2).GetProperties();
    PropertyInfo[] pi1 = typeof(T1).GetProperties();
    foreach (T1 t1Model in source)
    {
    model = Activator.CreateInstance<T2>();
    foreach (var p in pi)
    {
    foreach (var p1 in pi1)
    {
    if (p.Name == p1.Name)
    {
    p.SetValue(model, p1.GetValue(t1Model, null), null);
    break;
    }
    }
    }
    t2List.Add(model);
    }
    return t2List;
    }

    public static T2 ConvertToModel<T1, T2>(T1 source)
    {
    T2 model = default(T2);
    PropertyInfo[] pi = typeof(T2).GetProperties();
    PropertyInfo[] pi1 = typeof(T1).GetProperties();
    model = Activator.CreateInstance<T2>();
    foreach (var p in pi)
    {
    foreach (var p1 in pi1)
    {
    if (p.Name == p1.Name)
    {
    p.SetValue(model, p1.GetValue(source, null), null);
    break;
    }
    }
    }
    return model;
    }
    }

  • 相关阅读:
    GitHub Interesting Collection
    使用 CSS3 Flexible Boxes 布局
    消失的属性
    浅谈 JavaScript 模块化编程
    为你的 Javascript 加点咖喱
    软件测试
    osi七层模型
    3_Hydra(爆破神器)
    2_NC(瑞士军刀)
    1_HTTP协议详解
  • 原文地址:https://www.cnblogs.com/quwujin/p/11193563.html
Copyright © 2011-2022 走看看