using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AOWEN.Common
{
    public class DataBindCommon
    {
        /// <summary>
        /// 通过反射将C对象属性赋值到T对象上
        /// </summary>
        /// <typeparam name="T">被赋值对象类型</typeparam>
        /// <typeparam name="C">源对象类型</typeparam>
        /// <param name="source">源对象</param>
        /// <returns>T对象</returns>
        /// Author:杨祎
        /// CreateTime:2016-3-9
        public T PopulateEntityFromOther<T, C>(C source) where T : new()
        {
            if (source == null)
                return default(T);
            T entity = new T();
            Type typeT = typeof(T);
            Type typeC = typeof(C);
            PropertyInfo[] tPiList = typeT.GetProperties();
            PropertyInfo[] cPiList = typeC.GetProperties();
            foreach (PropertyInfo item in tPiList)
            {
                foreach (PropertyInfo cp in cPiList)
                {
                    if (cp.Name == item.Name)
                    {
                        if (item.PropertyType.Name.Contains("Nullable"))
                            item.SetValue(entity, cp.GetValue(source, null));
                        else
                            item.SetValue(entity, Convert.ChangeType(cp.GetValue(source, null), item.PropertyType), null);
                        break;
                    }
                }
                if (item.GetValue(entity)==null)
                {
                    if (item.PropertyType.Name.ToLower().Contains("string"))
                    {
                        item.SetValue(entity, "");
                    }
                }
            }
            return entity;
        }
        /// <summary>
        /// 通过反射将C对象List属性赋值到T对象List上
        /// </summary>
        /// <typeparam name="T">被赋值对象类型</typeparam>
        /// <typeparam name="C">源对象类型</typeparam>
        /// <param name="sourceList">源对象 List</param>
        /// <returns>T对象 List</returns>
        /// Author:杨祎
        /// CreateTime:2016-3-9
        public List<T> PopulateListFromOther<T, C>(List<C> sourceList) where T : new()
        {
            if (sourceList == null)
                return null;
            List<T> list = new List<T>();
            foreach (var source in sourceList)
            {
                list.Add(this.PopulateEntityFromOther<T, C>(source));
            }
            return list;
        }
        /// <summary>
        /// 补全null值
        /// </summary>
        /// <typeparam name="T">源对象类型</typeparam>
        /// <param name="source">源对象</param>
        /// <returns>源对象</returns>
        /// Author:杨祎
        /// CreateTime:2016-3-31
        public T PopulateEntityNull<T>(T source) where T : new()
        {
            if (source == null)
                return default(T);
            Type typeT = typeof(T);
            PropertyInfo[] tPiList = typeT.GetProperties();
            foreach (PropertyInfo item in tPiList)
            {
                if (item.GetValue(source) == null)
                {
                    if (item.PropertyType.Name.ToLower().Contains("string"))
                    {
                        item.SetValue(source, "");
                    }
                }
            }
            return source;
        }
    }
}
