对象复制ModelCopier

public static class ModelCopier
{
    public static void CopyCollection<T>(IEnumerable<T> from, ICollection<T> to)
    {
        if (from == null || to == null || to.IsReadOnly)
        {
            return;
        }

        to.Clear();
        foreach (T element in from)
        {
            to.Add(element);
        }
    }

    public static void CopyModel(object from, object to, string[] excludeProperties)
    {
        if (from == null || to == null)
        {
            return;
        }

        PropertyDescriptorCollection fromProperties = TypeDescriptor.GetProperties(from);
        PropertyDescriptorCollection toProperties = TypeDescriptor.GetProperties(to);

        foreach (PropertyDescriptor fromProperty in fromProperties)
        {
            PropertyDescriptor toProperty = toProperties.Find(fromProperty.Name, true /* ignoreCase */);
            if (toProperty != null && excludeProperties.Contains(toProperty.Name) == false && !toProperty.IsReadOnly)
            {
                // Can from.Property reference just be assigned directly to to.Property reference?
                bool isDirectlyAssignable = toProperty.PropertyType.IsAssignableFrom(fromProperty.PropertyType);
                // Is from.Property just the nullable form of to.Property?
                bool liftedValueType = (isDirectlyAssignable) ? false : (Nullable.GetUnderlyingType(fromProperty.PropertyType) == toProperty.PropertyType);

                if (isDirectlyAssignable || liftedValueType)
                {
                    object fromValue = fromProperty.GetValue(from);
                    if (isDirectlyAssignable || (fromValue != null && liftedValueType))
                    {
                        toProperty.SetValue(to, fromValue);
                    }
                }
            }
        }
    }
}

 

posted @ 2024-04-12 12:35  shiningrise  阅读(3)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css