zoukankan      html  css  js  c++  java
  • C# 对象复制

    /// <summary>
    /// 把DataTable对象转成List<T>对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="table"></param>
    /// <returns></returns>
    public static List<T> ConvertToModel<T>(this DataTable table) where T : new()
    {
        var list = new List<T>();
        PropertyInfo[] propertys = typeof(T).GetProperties();
    
        foreach (DataRow row in table.Rows)
        {
            T model = Activator.CreateInstance<T>();
            foreach (PropertyInfo pi in propertys)
            {
                var tempName = pi.Name;
                if (!table.Columns.Contains(tempName)) continue;
                if (!pi.CanWrite) continue;
                object value = Convert.ToString(row[tempName]);
                if (value != DBNull.Value)
                {
                    pi.SetValue(model, value, null);
                }
            }
    
            list.Add(model);
        }
        return list;
    }
    
    /// <summary>
    /// 把具有同结构的实体赋值到另外一个实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="from"></param>
    /// <returns></returns>
    public static T ConvertTo<T>(this object from) where T : new()
    {
        if (from == null)
        {
            return default(T);
        }
        var to = Activator.CreateInstance<T>();
        return from.CopyTo<T>(to);
    }
    
    /// <summary>
    /// 通过比较属性名称来给目标实体赋值
    /// </summary>
    /// <typeparam name="T">泛型实体</typeparam>
    /// <param name="from">原实体</param>
    /// <param name="to">目标实体</param>
    /// <returns></returns>
    public static T CopyTo<T>(this object from, T to) where T : new()
    {
        if (from == null)
        {
            return to;
        }
        if (to == null)
        {
            to = Activator.CreateInstance<T>();
        }
        PropertyDescriptorCollection fromProperties = TypeDescriptor.GetProperties(from);
        PropertyDescriptorCollection toProperties = TypeDescriptor.GetProperties(to);
        foreach (PropertyDescriptor fromProperty in fromProperties)
        {
            PropertyDescriptor toProperty = toProperties.Find(fromProperty.Name, true);
            if (toProperty != null && !toProperty.IsReadOnly)
            {
                bool isDirectlyAssignable = toProperty.PropertyType.IsAssignableFrom(fromProperty.PropertyType);
                bool liftedValueType = !isDirectlyAssignable && Nullable.GetUnderlyingType(fromProperty.PropertyType) == toProperty.PropertyType;
                if (isDirectlyAssignable || liftedValueType)
                {
                    object fromValue = fromProperty.GetValue(from);
                    if (isDirectlyAssignable || (fromValue != null && liftedValueType))
                    {
                        toProperty.SetValue(to, fromValue);
                    }
                }
            }
        }
        return to;
    }
    
    /// <summary>
    /// 把实体转换成一个dynamic对象
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static dynamic ToDynamic(this object value)
    {
        IDictionary<string, object> expando = new ExpandoObject();
    
        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
        {
            expando.Add(property.Name, property.GetValue(value));
        }
    
        return expando as ExpandoObject;
    }
  • 相关阅读:
    剑指Offer解题报告(Java版)——约瑟夫环 45
    剑指Offer解题报告(Java版)——扑克牌顺子 44
    剑指Offer解题报告(Java版)——n个骰子的点数 43
    基础知识:HashTable和HashMap的区别
    基础知识:Arraylist、vector、Linkedlist的比较
    第7章 SportsStorePeta 一个真实的应用程序
    第24章 捆绑包
    第23章 模型验证
    第22章 模型绑定
    第21章 URL和Ajax辅助器方法
  • 原文地址:https://www.cnblogs.com/hellowzl/p/10443234.html
Copyright © 2011-2022 走看看