c# 复制对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #region 复制对象 #region MyRegion private static Dictionary< string , object > _Dic = new Dictionary< string , object >(); public static TOut TransExp<TIn, TOut>(TIn tIn) { string key = string .Format( "trans_exp_{0}_{1}" , typeof (TIn).FullName, typeof (TOut).FullName); if (!_Dic.ContainsKey(key)) { ParameterExpression parameterExpression = Expression.Parameter( typeof (TIn), "p" ); List<MemberBinding> memberBindingList = new List<MemberBinding>(); foreach ( var item in typeof (TOut).GetProperties()) { if (!item.CanWrite) continue ; MemberExpression property = Expression.Property(parameterExpression, typeof (TIn).GetProperty(item.Name)); MemberBinding memberBinding = Expression.Bind(item, property); memberBindingList.Add(memberBinding); } MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New( typeof (TOut)), memberBindingList.ToArray()); Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression }); Func<TIn, TOut> func = lambda.Compile(); _Dic[key] = func; } return ((Func<TIn, TOut>)_Dic[key])(tIn); } #endregion /// <summary> /// 将一个对象的属性给另一个对象的相同属性赋值 /// </summary> /// <typeparam name="S">源对象类型</typeparam> /// <typeparam name="T">目标对象类型</typeparam> /// <param name="s">源对象</param> /// <param name="t">目标对</param> public static void AutoMapping<S, T>(S s, T t) { try { #region 【方法1】历史使用方法,在2022年2月8日11:45:39停止使用,使用方法2 //if (s == null) //{ // s = default; // return; //} //if (t == null) //{ // t = default; // return; //} //PropertyInfo[] pps = GetPropertyInfos(s.GetType()); //Type target = typeof(T); //foreach (var pp in pps) //{ // PropertyInfo targetPP = target.GetProperty(pp.Name); // object value = pp.GetValue(s, null); // if (targetPP != null && value != null) // { // try // { // targetPP.SetValue(t, value, null); // } // catch (Exception) // { // } // } //} #endregion #region 【方法2】,2022-2-8 11:46:40开始启用 t = Com.Domain.Common.DataConvert.TransExp<S, T>(s); #endregion } catch (Exception) { } } /// <summary> /// 获取Service层的类实例,在没有写配置文件时使用 /// </summary> /// <typeparam name="T">当前传入的类名</typeparam> /// <returns>类实体</returns> public static T CreateInstance<T>() where T : new () { return new T(); } public static PropertyInfo[] GetPropertyInfos(Type type) { return type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } #endregion |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端