Exemplo n.º 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, string[] filter = null) where T : class, new()
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     if (target == null)
     {
         target = new T();
     }
     ObjectCast.GetCast(source.GetType(), typeof(T)).Cast(source, target, filter);
     return(target);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 获取要转换的当前转换类实例
        /// </summary>
        /// <param name="sourceType">要转换的源类型</param>
        /// <param name="targetType">目标类型</param>
        /// <returns></returns>
        public static ObjectCast GetCast(Type sourceType, Type targetType)
        {
            Dictionary <Type, ObjectCast> casts = GetModuleCast(sourceType);
            ObjectCast result;

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