From 520abc3454cddbcd6bcb71eab6e5bc8ee9f86cc8 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Thu, 7 Apr 2022 16:20:14 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Object=E6=89=A9=E5=B1=95?=
=?UTF-8?q?=E6=96=B9=E6=B3=95CloneTo=E5=92=8CCloneFrom=E6=96=B9=E6=B3=95?=
=?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=B0=86=E5=8E=9F=E5=AF=B9=E8=B1=A1=E5=B1=9E?=
=?UTF-8?q?=E6=80=A7=E6=B5=85=E8=A1=A8=E5=A4=8D=E5=88=B6=E5=88=B0=E7=9B=AE?=
=?UTF-8?q?=E6=A0=87=E5=AF=B9=E8=B1=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi.Test/ObjectExtendTest.cs | 58 ++++++++++++++++++++++++
Falcon.SugarApi/ObjectExtend.cs | 42 +++++++++++++++++
2 files changed, 100 insertions(+)
create mode 100644 Falcon.SugarApi.Test/ObjectExtendTest.cs
create mode 100644 Falcon.SugarApi/ObjectExtend.cs
diff --git a/Falcon.SugarApi.Test/ObjectExtendTest.cs b/Falcon.SugarApi.Test/ObjectExtendTest.cs
new file mode 100644
index 0000000..0eb318a
--- /dev/null
+++ b/Falcon.SugarApi.Test/ObjectExtendTest.cs
@@ -0,0 +1,58 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Falcon.SugarApi;
+
+namespace Falcon.SugarApi.Test
+{
+ [TestClass]
+ public class ObjectExtendTest
+ {
+ [TestMethod]
+ public void CloneToTest() {
+ var s = new SourceClass { };
+ var t = new TargetClass { };
+ t.ica.ItemA = "itemb";
+
+ Assert.IsTrue(t.ica.ItemA == "itemb");
+
+ var r = s.CloneTo(t);
+
+ Assert.IsNotNull(s);
+ Assert.IsNotNull(r);
+ Assert.IsNotNull(t);
+ Assert.IsTrue(s.ia == t.ia, $"a.id:{s.ia},t.ia:{t.ia}");
+ Assert.IsTrue(t.ia == 1);
+ Assert.IsTrue(s.sa == t.sa);
+ Assert.IsTrue(s.sc == "sc");
+ Assert.IsTrue(t.sd == "sd");
+ Assert.IsTrue(s.ica.ItemA == "itema");
+ Assert.IsTrue(t.ica.ItemA == "itema");
+ Assert.IsTrue(t.ica.Equals(s.ica));
+ }
+ }
+
+ public class SourceClass
+ {
+ public int ia { get; set; } = 1;
+ public string sa { get; set; } = "sa";
+ public string sc { get; set; } = "sc";
+ public ItemClass ica { get; set; } = new ItemClass();
+ }
+
+ public class TargetClass
+ {
+ public int ia { get; set; }
+ public string sa { get; set; }
+ public string sd { get; set; } = "sd";
+ public ItemClass ica { get; set; } = new ItemClass();
+ }
+
+ public class ItemClass
+ {
+ public string ItemA { get; set; } = "itema";
+ }
+}
diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs
new file mode 100644
index 0000000..805881b
--- /dev/null
+++ b/Falcon.SugarApi/ObjectExtend.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+
+namespace Falcon.SugarApi
+{
+ ///
+ /// Object类型扩展方法
+ ///
+ public static class ObjectExtend
+ {
+ ///
+ /// 对source进行浅表复制,并复制到target中
+ ///
+ /// 原对象类型
+ /// 原对象
+ /// 目标对象
+ public static TSource CloneTo(this TSource source, [NotNull] object target) where TSource : class {
+ _ = source ?? throw new ArgumentNullException(nameof(source));
+ _ = target ?? throw new ArgumentNullException(nameof(target));
+ var all = from s in source.GetType().GetProperties()
+ join t in target.GetType().GetProperties() on s.Name equals t.Name
+ select new { s, t };
+ foreach (var item in all) {
+ item.t.SetValue(target, Convert.ChangeType(item.s.GetValue(source), item.s.PropertyType));
+ }
+ return source;
+ }
+
+ ///
+ /// 从原对象中浅表复制属性值
+ ///
+ /// 目标对象类型
+ /// 目标对象
+ /// 原对象
+ /// 目标对象
+ public static Ttarget CloneFrom(this Ttarget target, object source) where Ttarget : class {
+ source.CloneTo(target);
+ return target;
+ }
+ }
+}