zoukankan      html  css  js  c++  java
  • 转换实体对象 与string转化数组

         /// <summary>
            /// 转换实体对象
            /// </summary>
            /// <typeparam name="T1">转化的实体</typeparam>
            /// <typeparam name="T2">转换后的实体</typeparam>
            /// <param name="t1"></param>
            /// <param name="t2"></param>
            /// <returns></returns>
            public T2 ParstModel<T1, T2>(T1 t1, T2 t2)
            {
    
                try
                {
                    if (t1 == null)
                    {
                        return t2;
                    }
                    else
                    {
                        System.Reflection.PropertyInfo[] properties = t1.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                        if (properties.Length <= 0)
                        {
                            return t2;
                        }
                        foreach (System.Reflection.PropertyInfo item in properties)
                        {
                            string name = item.Name;
                            object value = item.GetValue(t1, null);
                            if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                            {
                                t2.GetType().GetProperty(name).SetValue(t2, value, null);
                            }
                            else
                            {
                                ParstModel(value, t2);
                            }
                        }
                    }
                    return t2;
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
            }
    
            /// <summary>
            ///string转为数组
            /// </summary>
            /// <param name="str">要转化的string对象</param>
            /// <param name="arr">返回的string数组</param>
            /// <returns></returns>
            public string[] StrSplitToarry(string str, string[] arr)
            {
                if (str.Contains(','))
                {
                    arr = str.Split(',');
                }
                else
                {
                    Array.Resize(ref arr, 1);
                    arr[0] = str;
                }
                return arr;
            }
  • 相关阅读:
    实际项目管理-1
    arcengine 错误
    一些视频技术类网站
    winform 组件之dotnetbar10.5.3
    winform 弹框的组件
    一个好的开源网站
    写webservice 注意点
    ww
    js
    瀑布流
  • 原文地址:https://www.cnblogs.com/TzH-Sky/p/3910411.html
Copyright © 2011-2022 走看看