日期:2014-05-18 浏览次数:21134 次
 /// <summary>
    /// 属性赋值
    /// </summary>
    /// <typeparam name="T">目标属性</typeparam>
    public static class MyProperty<T>
    {
        public static T PropertyCopy(object source, T target)
        {
            Type sourceType = source.GetType();
            Type targetType = target.GetType();
            PropertyInfo[] pis = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in pis)
            {
                string propertyName = pi.Name;
                PropertyInfo pit = targetType.GetProperty(propertyName);
                if (pit != null)
                {
                    pit.SetValue(target, pi.GetValue(source, null), null);
                }
            }
            return target;
        }
    }