Exemplo n.º 1
0
        /// <summary>
        /// 克隆一个Entity对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T CloneEntity <T>(T entity)
            where T : EntityBase
        {
            if (entity == null)
            {
                return(default(T));
            }
            Type   type  = entity.GetType();
            object t     = GetFastInstanceCreator(type)();
            var    fiels = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            var    list  = new List <FieldInfo>(fiels);

            foreach (PropertyInfo p in type.GetProperties())
            {
                if (!CanUseType(p.PropertyType))
                {
                    continue;                              //shallow only
                }
                object value = GetPropertyValue(entity, p.Name);
                if (value == null)
                {
                    continue;
                }

                //对属性赋值
                FieldInfo field = list.Find(f => f.Name == "_" + p.Name);
                if (field != null)
                {
                    field.SetValue(t, value);
                }
            }

            return((T)t);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 快速创建一个T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="type"></param>
 /// <returns></returns>
 public static T CreateInstance <T>(Type type)
 {
     if (!type.IsPublic)
     {
         return((T)Activator.CreateInstance(type));
     }
     else
     {
         return((T)GetFastInstanceCreator(type)());
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 快速创建一个T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static T CreateInstance <T>()
 {
     return((T)GetFastInstanceCreator(typeof(T))());
 }