Tag: system.reflection ilgenerator

将一个对象属性值转移到另一个对象属性值

最重要的是,我知道AutoMapper ,我不想使用它。 因为我正在学习C# ,我想深入了解它。 所以我正试着自己做这个问题(下面解释)。 但是,我正在尝试创建一个属性复制器来处理一种类型属性的值到另一种属性,如果该属性具有相同的名称和类型,并且可以从源中读取并在目标中可写。 我正在使用type.GetProperties()方法。 采样方法如下: static void Transfer(object source, object target) { var sourceType = source.GetType(); var targetType = target.GetType(); var sourceProps = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance); var targetProps = (from t in targetType.GetProperties() where t.CanWrite && (t.GetSetMethod().Attributes & MethodAttributes.Static) == 0 select t).ToList(); foreach(var prop in sourceProps) { var value = prop.GetValue(source, […]