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

 

posted on 2019-02-27 13:50  wangzhiliang  阅读(319)  评论(0编辑  收藏  举报

导航