namespace WcsMain.Plugins; /// /// object 拷贝 /// public class ObjectCopy { /// /// 将一个类拷贝到另一个类 /// /// /// /// /// public static OUT CopyProperties(IN input) where OUT : class, new() where IN : class, new() { // 输出 OUT newClass = Activator.CreateInstance(); Type outType = newClass.GetType(); var outProperties = outType.GetProperties(); if (outProperties == default || outProperties.Length == 0) return newClass; // 输入 Type inType = typeof(IN); var inProperties = inType.GetProperties(); foreach ( var inProperty in inProperties) { var outPropertie = outProperties.FirstOrDefault(s => s.Name == inProperty.Name); if(outPropertie == default) continue; outPropertie.SetValue(newClass, inProperty.GetValue(input)); } return newClass; } }