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
    
                    }
                }
    
    
            }

    --

  • 相关阅读:
    如何搜索 git 提交记录
    使用Mongo进行分页
    mongodb 数据自动备份
    linux 添加环境变量
    centos7安装bbr
    centos7安装node
    [shell]输出内容到剪切板
    centos 7 已经开启 22 端口但无法连接
    一些有趣的 js 包
    机房选择
  • 原文地址:https://www.cnblogs.com/runliuv/p/10767034.html
Copyright © 2011-2022 走看看