Пример #1
0
        public static TypeMappingDescription Get(Type type)
        {
            if (type == null)
            {
                return(null);
            }

            if (_cacheList.Keys.Contains(type))
            {
                return(_cacheList[type]);
            }
            else
            {
                TypeMappingDescription cacheCodon = new TypeMappingDescription(type);

                Monitor.Enter(_cacheList);

                if (_cacheList.Keys.Contains(type) == false)
                {
                    _cacheList.Add(type, cacheCodon);
                }

                Monitor.Exit(_cacheList);

                return(cacheCodon);
            }
        }
Пример #2
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enterprises.Framework.Plugin.Mapper
{
    /// <summary>
    /// 拷贝行为只针对 sourceObject 和 targetObject 所共有的属性
    /// 在 sourceObject 和 targetObject 中的待拷贝的属性值的类型处理:
    ///     如果是值类型,直接拷贝,如果是引用类型,sourceObject 中的属性的类型 必须 和 targetObject 中的属性的类型一致,或是它的派生类
    /// 如果要支持类型不一致的属性自动进行类型转换,你可以在 PropertyMappingDescription 这个类中实现转换器功能
    /// 拷贝行为 不会 改变 targetObject 中不需要被拷贝的属性的值
    /// 你可以组合使用几个方法来从多个对象中拷贝指定的属性值到一个 targetObject
    /// </summary>
    public class ShengMapper
    {
        /// <summary>
        /// 拷贝两个对象所共有的全部属性
        /// </summary>
        /// <param name="sourceObject"></param>
        /// <param name="targetObject"></param>
        public static void SetValues(object sourceObject, object targetObject)
        {
            SetValues(sourceObject, targetObject, null, null);
        }

        /// <summary>
        /// 拷贝指定的 properties
        /// 如果指定的属性不存在于 sourceObject 或 targetObject,将跳过不存在的属性
        /// </summary>
        /// <param name="sourceObject"></param>
        /// <param name="targetObject"></param>
        /// <param name="properties"></param>
        public static void SetValuesWithProperties(object sourceObject, object targetObject, string[] properties)
        {
            SetValues(sourceObject, targetObject, properties, null);
        }

        /// <summary>
        /// 拷贝两个对象所共有的全部属性,除了指定的 properties
        /// </summary>
        /// <param name="sourceObject"></param>
        /// <param name="targetObject"></param>
        /// <param name="properties"></param>
        public static void SetValuesWithoutProperties(object sourceObject, object targetObject, string[] properties)
        {
            SetValues(sourceObject, targetObject, null, properties);
        }

        private static void SetValues(object sourceObject, object targetObject, string[] withProperties, string[] withoutProperties)
        {

            if (sourceObject == null || targetObject == null)
                throw new ArgumentNullException();

            Type sourceObjectType = sourceObject.GetType();
            Type targetObjectType = targetObject.GetType();

            TypeMappingDescription sourceObjectTypeCache = TypeMappingCache.Get(sourceObjectType);
            TypeMappingDescription targetObjectCache = TypeMappingCache.Get(targetObjectType);

            foreach (PropertyMappingDescription sourceProperty in sourceObjectTypeCache.PropertyList)
            {
                if (withProperties != null && withProperties.Length > 0 && withProperties.Contains(sourceProperty.Name) == false)
                    continue;

                if (withoutProperties != null && withoutProperties.Length > 0 && withoutProperties.Contains(sourceProperty.Name))
                    continue;

                if (sourceProperty.CanRead == false)
                    continue;

                if (targetObjectCache.ContainsProperty(sourceProperty.Name) == false)
                    continue;

                object sourcePropertyValue = sourceObjectTypeCache.GetValue(sourceObject, sourceProperty.Name);
                targetObjectCache.SetValue(targetObject, sourceProperty.Name, sourcePropertyValue);
            }
        }
    }
}