zoukankan      html  css  js  c++  java
  • C# .NET MODEL 复制,实体类复制

    -- 

    /// <summary>
            /// 将T1 实体的参数复制给 T2 ,不能处理多层次
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <param name="a"></param>
            /// <param name="b"></param>
            public static void CopyModel<T1, T2>(T1 a, T2 b)
            {
    
                System.Reflection.PropertyInfo[] cfgItemProperties = a.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    
                foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
                {
                    string name = item.Name;
                    object value = item.GetValue(a, null);
                    //string 或 值属性,且value 不为 null
                    if ((item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && value != null && !string.IsNullOrEmpty(value.ToString()))
                    {
                        #region 在MODEL2中查找是否有此参数名,有则赋值
                        System.Reflection.PropertyInfo[] list2 = b.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                        foreach (System.Reflection.PropertyInfo i2 in list2)
                        {
                            //两者 PropertyType.Name  要相等
                            if (item.Name == i2.Name && item.PropertyType.Name == i2.PropertyType.Name)
                            {
                                i2.SetValue(b, value, null);
                            }
                        }
                        #endregion
    
                    }
                }
    
    
            }

    --

  • 相关阅读:
    C# Using MySQL
    C++ Asynchronous IO on Windows
    C++ Bind adapter usage
    C# Entity Framework with MSSQL, MYSQL
    Read a file into array for C++/C#
    上下移动 04.16
    盒子模型001基础
    JavaScript&JQ 001_五角星评分
    jQuery EasyUI tree的 使用
    自定义实现URL重写 04.18
  • 原文地址:https://www.cnblogs.com/runliuv/p/10767034.html
Copyright © 2011-2022 走看看