From 58c579309abc9ee5b5538ee5bfb11bf238261922 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 2 Mar 2020 15:14:00 +0100 Subject: [PATCH] Added Copy-Method --- PropertyCopy/Class.cs | 26 ++++++++++++++++++++++++++ PropertyCopy/Class1.cs | 8 -------- 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 PropertyCopy/Class.cs delete mode 100644 PropertyCopy/Class1.cs diff --git a/PropertyCopy/Class.cs b/PropertyCopy/Class.cs new file mode 100644 index 0000000..eb52625 --- /dev/null +++ b/PropertyCopy/Class.cs @@ -0,0 +1,26 @@ +using System; + +namespace PropertyCopy +{ + public static class Class + { + public static void CopyPropertiesFrom(this object self, object parent) + { + var fromProperties = parent.GetType().GetProperties(); + var toProperties = self.GetType().GetProperties(); + + foreach (var fromProperty in fromProperties) + { + foreach (var toProperty in toProperties) + { + if (fromProperty.Name == toProperty.Name && fromProperty.PropertyType == toProperty.PropertyType) + { + toProperty.SetValue(self, fromProperty.GetValue(parent)); + break; + } + } + } + } + + } +} diff --git a/PropertyCopy/Class1.cs b/PropertyCopy/Class1.cs deleted file mode 100644 index 07d6eca..0000000 --- a/PropertyCopy/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace PropertyCopy -{ - public class Class1 - { - } -}