Exemple #1
0
        /// <summary>
        /// 拷贝当前对象的属性值到目标对象上
        /// </summary>
        /// <typeparam name="T">目标对象类型</typeparam>
        /// <param name="source">当前对象</param>
        /// <returns>返回赋值过后的目标对象</returns>
        public static T CopyTo <T>(this object source) where T : class, new()
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            T target = new T();

            ModelCast.GetCast(source.GetType(), typeof(T)).Cast(source, target);
            return(target);
        }
Exemple #2
0
        /// <summary>
        /// 获取要转换的当前转换类实例
        /// </summary>
        /// <param name="sourceType">要转换的源类型</param>
        /// <param name="targetType">目标类型</param>
        /// <returns></returns>
        public static ModelCast GetCast(Type sourceType, Type targetType)
        {
            Dictionary <Type, ModelCast> casts = GetModuleCast(sourceType);
            ModelCast result;

            lock (casts)
            {
                if (!casts.TryGetValue(targetType, out result))
                {
                    result = new ModelCast(sourceType, targetType);
                    casts.Add(targetType, result);
                }
            }
            return(result);
        }
Exemple #3
0
 /// <summary>
 /// 转换对象
 /// </summary>
 /// <typeparam name="TSource">源类型</typeparam>
 /// <typeparam name="TTarget">目标类型</typeparam>
 /// <param name="source">源对象</param>
 /// <param name="target">目标对象</param>
 public static void CastObject <TSource, TTarget>(TSource source, TTarget target)
     where TSource : class
     where TTarget : class
 {
     ModelCast.GetCast(typeof(TSource), typeof(TTarget)).Cast(source, target);
 }