C# 利用反射拷貝類

來源:互聯網
上載者:User

標籤:bool   using   for   continue   scribe   程式員   target   write   快樂的程式員   

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Reflection;namespace ReflectTools{    class Program    {        static void Main(string[] args)        {            Person a = new Person            {                Name = "盤子臉1號",                Info = new Details                    {                        IsIT = false,                        IsMarriage = true,                        Code = "Hello world 那是不可能的 草泥馬"                    },                PersonInfo = new CodeWorker                {                    Describe = "我是一個快樂的程式員"                }            };            PersonVI vi = Program.CloneClass<PersonVI>(a);            Console.ReadLine();        }        public static T CloneClass<T>(Object sourceObj) where T : class,new()        {            Type type = typeof(T);            object targetObj = Activator.CreateInstance(type);            Type targetType = targetObj.GetType();            Type sourceType = sourceObj.GetType();            CloneWork(sourceType, targetType, sourceObj, targetObj);            return (T)targetObj;        }        public static void CloneWork(Type sourceType, Type targetType, object sourceObj, object targetObj)        {            FieldInfo[] info = sourceType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            FieldInfo targetInfo = null;            object val = null;            for (int i = 0; i < info.Length; i++)            {                targetInfo = targetType.GetField(info[i].Name);                if (targetInfo == null)                {                    Console.WriteLine(info[i].Name + " 沒找到");                    continue;                }                if (targetInfo.FieldType.IsClass)                {                    if (!(targetInfo.FieldType.Name == "String" || targetInfo.FieldType.FullName == "System.String"))                    {                        if (targetInfo.GetValue(targetObj) == null)                        {                            targetInfo.SetValue(targetObj, Activator.CreateInstance(targetInfo.FieldType));                        }                        //說明是普通的類                        CloneWork(info[i].GetValue(sourceObj).GetType(), targetInfo.FieldType, info[i].GetValue(sourceObj), targetInfo.GetValue(targetObj));                    }                }                if (targetInfo != null)                {                    val = info[i].GetValue(sourceObj);                    targetInfo.SetValue(targetObj, val);                }            }        }    }    public class Person    {        public string Name;        public Details Info;        public CodeWorker PersonInfo;    }    public class Details    {        public bool IsMarriage;        public bool IsIT;        public String Code;    }    public class CodeWorker    {        public String Describe;    }    public class PersonVI    {        public string Name;        public Details Info;        public CodeWorker PersonInfo;    }}

C# 利用反射拷貝類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.