Пример #1
0
        /// <summary>
        /// 复制数据
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <typeparam name="K">拥有索引器的实体类型</typeparam>
        /// <typeparam name="P">索引器参数类型,例:int</typeparam>
        /// <typeparam name="R">索引器返回类型,例:string</typeparam>
        /// <param name="originalData">原实体数据</param>
        /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param>
        /// <param name="reflectionType">反射类型</param>
        /// <returns></returns>
        public static T Copy <T, K, P, R>(K originalData, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
            where T : class, new()
            where K : class
        {
            T t = ReflectionGenericHelper.New <T>();

            Func <K, P, object> indexCall = ReflectionGenericHelper.PropertyIndexGetCall <K, P, R>(reflectionType);
            Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), typeof(T), propertyMatchList, null);

            foreach (var keyValueItem in propertyMatchDict)
            {
                P indexName = (P)Convert.ChangeType(keyValueItem.Value, typeof(P), CultureInfo.InvariantCulture);
                ReflectionHelper.SetPropertyValue(t, indexCall(originalData, indexName).ToString(), keyValueItem.Key);
            }
            return(t);
        }