Exemple #1
0
 /// <summary>
 /// 将当前对象的属性值复制到目标对象,使用浅表复制
 /// </summary>
 /// <typeparam name="T">目标对象类型</typeparam>
 /// <param name="source">源对象</param>
 /// <param name="target">目标对象,如果为空,将生成一个</param>
 /// <returns>复制过后的目标对象</returns>
 public static T CopyTo <T>(this object source, T target = null) where T : class, new()
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     if (target == null)
     {
         target = new T();
     }
     ModuleCast.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 ModuleCast GetCast(Type sourceType, Type targetType)
        {
            Dictionary <Type, ModuleCast> casts = GetModuleCast(sourceType);
            ModuleCast result;

            lock (casts)
            {
                if (!casts.TryGetValue(targetType, out result))
                {
                    result = new ModuleCast(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
 {
     ModuleCast.GetCast(typeof(TSource), typeof(TTarget)).Cast(source, target);
 }