diff --git a/Jx.Toolbox.Test/ObjectTest.cs b/Jx.Toolbox.Test/ObjectTest.cs index 2fc3466..969f36c 100644 --- a/Jx.Toolbox.Test/ObjectTest.cs +++ b/Jx.Toolbox.Test/ObjectTest.cs @@ -18,4 +18,14 @@ public void SetPropertyTest() Assert.Throws(() => { this.SetProperty("abc", 1);}); Assert.Throws(() => { this.SetProperty("ListTest", 1);}); } + + [Fact] + public void CopyTest() + { + this.SetProperty(nameof(Test), "test"); + this.SetProperty(nameof(IntTest), 1); + var obj = this.CopyTo(); + Assert.Equal("test", obj.Test); + Assert.Equal(1, obj.IntTest); + } } \ No newline at end of file diff --git a/Jx.Toolbox/Extensions/ObjectExtension.cs b/Jx.Toolbox/Extensions/ObjectExtension.cs index 730b7aa..95c3687 100644 --- a/Jx.Toolbox/Extensions/ObjectExtension.cs +++ b/Jx.Toolbox/Extensions/ObjectExtension.cs @@ -88,5 +88,18 @@ public static void CopyFrom(this TSource source, TTarget targe { CopyTo(target, source); } + + /// + /// 将源属性拷贝到目标 + /// + /// + /// + /// + public static TTarget CopyTo(this object source) + { + var target = Activator.CreateInstance(); + CopyTo(source, target); + return target; + } } } \ No newline at end of file diff --git a/Jx.Toolbox/Jx.Toolbox.csproj b/Jx.Toolbox/Jx.Toolbox.csproj index 3a328f8..fd5d0d0 100644 --- a/Jx.Toolbox/Jx.Toolbox.csproj +++ b/Jx.Toolbox/Jx.Toolbox.csproj @@ -9,7 +9,7 @@ https://github.com/j4587698/Jx.Toolbox MIT tool - 0.3.5 + 0.3.6