diff --git a/Faclon.Extend/ObjectCopyToOption.cs b/Faclon.Extend/ObjectCopyToOption.cs new file mode 100644 index 0000000..76f5d3c --- /dev/null +++ b/Faclon.Extend/ObjectCopyToOption.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Faclon.Extend +{ + /// + /// 对象复制选项 + /// + public class ObjectCopyToOption + { + /// + /// 获取默认值 + /// + public static ObjectCopyToOption Default { + get { + return new ObjectCopyToOption(); + } + } + } +} diff --git a/Faclon.Extend/ObjectExtend.cs b/Faclon.Extend/ObjectExtend.cs new file mode 100644 index 0000000..d36921c --- /dev/null +++ b/Faclon.Extend/ObjectExtend.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Faclon.Extend +{ + /// + /// Object扩展 + /// + public static class ObjectExtend + { + /// + /// 将当前对象属性赋值到目标对象属性中。 + /// + /// 当前对象 + /// 目标属性 + /// 复制选项 + public static void CopyTo(this object source,object target,ObjectCopyToOption option = null) { + if(source == null) + throw new ArgumentNullException(nameof(source)); + if(target == null) + throw new ArgumentNullException(nameof(target)); + option = option ?? ObjectCopyToOption.Default; + foreach(var ps in source.GetType().GetProperties()) { + if(ps.CanRead) { + var pt = target.GetType().GetProperty(ps.Name); + if(pt.CanWrite && ps.PropertyType.IsAssignableFrom(pt.PropertyType)) { + pt.SetValue(target,ps.GetValue(source,null),null); + } + } + } + } + + /// + /// 根据提供的更新方法更新目标对象属性 + /// + /// 原对象类型 + /// 目标对象类型 + /// 原对象 + /// 目标对象 + /// 转换方法 + public static void CopyTo(this TSource source,TTarget target,Action action) { + action(source,target); + } + } +} diff --git a/Faclon.ExtendTests/ObjectExtendTests.cs b/Faclon.ExtendTests/ObjectExtendTests.cs new file mode 100644 index 0000000..08404fd --- /dev/null +++ b/Faclon.ExtendTests/ObjectExtendTests.cs @@ -0,0 +1,46 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Faclon.Extend; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Faclon.Extend.Tests +{ + [TestClass()] + public class ObjectExtendTests + { + [TestMethod()] + public void CopyToTest() { + var o1 = new Obj1 { + Id = 1, + Name = "Tom", + BirthDay = new DateTime(2019,8,5), + }; + var o2 = new Obj2 { + BirthDay = DateTimeOffset.Parse("2019-8-5"), + }; + //测试一般复制 + o1.CopyTo(o2); + Assert.IsTrue(o2.Id == o1.Id,"int 复制错误"); + Assert.IsTrue(o2.Name == o1.Name,"string 复制错误"); + Assert.IsTrue(o2.BirthDay.Year == o1.BirthDay.Year,"DateTime到DateTimeOffset复制错误"); + Assert.IsTrue(o2.BirthDay.Month == o1.BirthDay.Month,"DateTime到DateTimeOffset复制错误"); + Assert.IsTrue(o2.BirthDay.Day == o1.BirthDay.Day,"DateTime到DateTimeOffset复制错误"); + } + } + + class Obj1 + { + public int Id { get; set; } + public string Name { get; set; } + + public DateTime BirthDay { get; set; } + } + + class Obj2 + { + public int Id { get; set; } + public string Name { get; set; } + public DateTimeOffset BirthDay { get; set; } + } +} \ No newline at end of file