Same attributes and field copy between classes

Source: Internet
Author: User

It is mainly used to assign values to members of different classes. Saves the amount of code.

namespace ConsoleApplication3{    public interface interTest    {    }    public class Test : interTest    {        public string Name;        public int Id;        public int TestId;        public int Property { get; set; }    }    public class TestClone    {        public string Name;        public int Id;        public int CloneId;        public int Property { get; set; }    }    public static class Logic    {        public static MemberTypes AllowType = MemberTypes.Field | MemberTypes.Property;        public static void CloneField<T, T1>(this T instance, T1 copyTo) where T : class ,interTest, new()        {            var instanceType = instance.GetType();            var copyType = copyTo.GetType();            var copyField = copyType.GetFields();            instanceType.GetFields().All(field =>            {                var temp = copyField.FirstOrDefault(o => o.Name.Equals(field.Name, StringComparison.CurrentCulture) && o.FieldType == field.FieldType);                if (temp == null) return true;                temp.SetValue(copyTo, field.GetValue(instance));                return true;            });        }        public static void CloneProperty<T, T1>(this T instance, T1 copyTo) where T : class ,interTest, new()        {            var instanceType = instance.GetType();            var copyType = copyTo.GetType();            var copyProperties = copyType.GetProperties();            instanceType.GetProperties().All(property =>            {                var temp = copyProperties.FirstOrDefault(o => o.Name.Equals(property.Name, StringComparison.CurrentCulture) && o.PropertyType == property.PropertyType);                if (temp == null) return true;                temp.SetValue(copyTo, property.GetValue(instance, null), null);                return true;            });        }    }}

 

Call
class Program    {        static void Main(string[] args)        {            Test t = new Test();            t.Id = 1;            t.Name = "shikyoh";            t.TestId = 12;            t.Property = 600;            TestClone tc = new TestClone();            t.CloneField(tc);            t.CloneProperty(tc);                       Console.WriteLine("tc.Name:" + tc.Name);            Console.WriteLine("tc.Id:" + tc.Id);            Console.WriteLine("tc.Property:" + tc.Property);            Console.WriteLine("tc.CloneId:" + tc.CloneId);        }    }

 

Note that the class name must be the same and the type must be the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.