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;
    }
  • 相关阅读:
    第一次结对编程作业
    第7组 团队展示
    第一次个人编程作业
    js学习笔记(1)
    第一次博客作业
    期末总结
    王者光耀团队作业
    第四次c++作业
    c++第三次作业
    第一次编程作业
  • 原文地址:https://www.cnblogs.com/hellowzl/p/10443234.html
Copyright © 2011-2022 走看看