• EntityUtil类


    实现从一个实体向另一个实体拷贝。

    View Code
      1 //By:Jxb
      2  public class EntityUtil
      3     {
      4 
      5         /// <summary>
      6         /// 从一个对象中拷贝数据到另一个对象(属性名一致的才会拷贝,用于泛型实体)
      7         /// </summary>
      8         /// <param name="source"></param>
      9         /// <param name="destination"></param>
     10         public static void CloneDataList<TSource, TDestination>(List<TSource> sourcelist, List<TDestination> destinationlist)
     11         {
     12             if (sourcelist != null)
     13             {
     14                 for (int i = 0; i < sourcelist.Count; i++)
     15                 {
     16                     destinationlist.Add(Activator.CreateInstance<TDestination>());
     17                     for (int x = 0; x < destinationlist.Count; x++)
     18                     {
     19                         PropertyInfo[] destinationProperties = destinationlist[destinationlist.Count - 1].GetType().GetProperties();
     20                         PropertyInfo[] sourceProperties = sourcelist[i].GetType().GetProperties();
     21 
     22                         foreach (PropertyInfo property in destinationProperties)
     23                         {
     24                             PropertyInfo sourceProperty = sourcelist[i].GetType().GetProperty(property.Name);
     25                             if (sourceProperty != null)
     26                             {
     27                                 object value = sourceProperty.GetValue(sourcelist[i], null);
     28                                 try
     29                                 {
     30                                     property.SetValue(destinationlist[destinationlist.Count - 1], value, null);
     31                                 }
     32                                 catch { }
     33                             }
     34                         }
     35                         break;
     36                     }
     37                 }
     38             }
     39         }
     40 
     41 
     42         /// <summary>
     43         /// 从一个对象中拷贝数据到另一个对象(属性名一致的才会拷贝)
     44         /// </summary>
     45         /// <param name="source"></param>
     46         /// <param name="destination"></param>
     47         public static void CloneData<TSource, TDestination>(TSource source, TDestination destination)
     48         {
     49             if (source != null)
     50             {
     51                 PropertyInfo[] destinationProperties = destination.GetType().GetProperties();
     52                 PropertyInfo[] sourceProperties = source.GetType().GetProperties();
     53                 foreach (PropertyInfo property in destinationProperties)
     54                 {
     55                     PropertyInfo sourceProperty = source.GetType().GetProperty(property.Name);
     56                     if (sourceProperty != null)
     57                     {
     58                         object value = sourceProperty.GetValue(source, null);
     59                         try
     60                         {
     61                             property.SetValue(destination, value, null);
     62                         }
     63                         catch { }
     64                     }
     65                 }
     66             }
     67         }
     68 
     69         /// <summary>
     70         /// 从一个对象中拷贝数据到另一个对象(属性名一致的才会拷贝,用于泛型实体)
     71         /// </summary>
     72         /// <param name="source"></param>
     73         /// <param name="destination"></param>
     74         public static void CloneData<TSource, TDestination>(List<TSource> sourcelist, List<TDestination> destinationlist)
     75         {
     76             if (sourcelist != null)
     77             {
     78                 for (int i = 0; i < sourcelist.Count; i++)
     79                 {
     80                     destinationlist.Add(Activator.CreateInstance<TDestination>());
     81                     for (int x = 0; x < destinationlist.Count; x++)
     82                     {
     83                         PropertyInfo[] destinationProperties = destinationlist[destinationlist.Count - 1].GetType().GetProperties();
     84                         PropertyInfo[] sourceProperties = sourcelist[i].GetType().GetProperties();
     85 
     86                         foreach (PropertyInfo property in destinationProperties)
     87                         {
     88                             PropertyInfo sourceProperty = sourcelist[i].GetType().GetProperty(property.Name);
     89                             if (sourceProperty != null)
     90                             {
     91                                 object value = sourceProperty.GetValue(sourcelist[i], null);
     92                                 try
     93                                 {
     94                                     property.SetValue(destinationlist[destinationlist.Count - 1], value, null);
     95                                 }
     96                                 catch { }
     97                             }
     98                         }
     99                         break;
    100                     }
    101                 }
    102             }
    103         }
    104 
    105 
    106 
    107         /// <summary>
    108         /// 将实体里的属性与值,拷贝出来(如果值超过128个字符,则截取)
    109         /// </summary>
    110         /// <param name="destination"></param>
    111         public static string CloneDataToString(object destination)
    112         {
    113             StringBuilder sb = new StringBuilder();
    114             PropertyInfo[] destinationProperties = destination.GetType().GetProperties();
    115             foreach (PropertyInfo item in destinationProperties)
    116             {
    117                 string name = item.Name.Replace("\r\n", "");
    118                 object value = item.GetValue(destination, null);
    119 
    120                 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
    121                 {
    122                     if (value != null)
    123                     {
    124                         if (value.ToString().Length > 128)
    125                         {
    126                             value = value.ToString().Substring(0, 128);
    127                         }
    128                     }
    129                     sb.Append(string.Format("{0}:{1},", name, value));
    130                 }
    131             }
    132             if (sb.Length > 0)
    133             {
    134                 sb.Remove(sb.Length - 1, 1);
    135             }
    136             return sb.ToString();
    137         }
  • 相关阅读:
    git如何从远端获取某个文件
    git显示不出来图标标志
    sublime text3设置
    怎么解决sublime的插件自动被禁用
    外甥语录
    sublime Text3支持vue高亮,sublime Text3格式化Vue
    sass安装方法,绝对好用的方式
    win10 安装nodejs,报错there is a problem in the windows installer package
    npm下载模块提速方法
    npm如何删除node_modules文件夹
  • 原文地址:https://www.cnblogs.com/Loofah/p/2507222.html
Copyright © 2020-2023  润新知