/// <summary> /// 对着相同属性名称值 /// </summary> /// <typeparam name="D">新对象</typeparam> /// <typeparam name="S">原始对象</typeparam> /// <param name="s">原始对象</param> /// <param name="isCopyStruct">是否只复制结构类型(包括String类型)</param> /// <returns></returns> public static D MapperStruct<D, S>(S s,bool isCopyStruct = false) { D d = Activator.CreateInstance<D>(); try { var Types = s.GetType();//获得类型 var Typed = typeof(D); foreach (PropertyInfo sp in Types.GetProperties()) { foreach (PropertyInfo dp in Typed.GetProperties()) { var isStruct = true; if (isCopyStruct) { isStruct = (dp.PropertyType.Name.ToLower().Equals("string") || !dp.PropertyType.IsClass); } if (dp.Name == sp.Name && isStruct) { dp.SetValue(d, sp.GetValue(s, null), null); } } } } catch (Exception ex) { throw ex; } return d; }