zoukankan      html  css  js  c++  java
  • C# 利用反射拷贝类

    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++中操作符函数
    C++中的类
    大型网站系统架构的演化
    GitHub初体验(小菜新手github用起来)
    黄聪:Adobe CS4 中文版 完美破解版下载
    WIN7下回收站不小心删除的文件怎么恢复,免费数据恢复软件下载
    PowerDesigner使用教程
    黄聪:CodeSmith和PowerDesigner的使用安装和数据库创建(原创系列教程)
    ORACLE SEQUENCE用法
    C++ DirectUI库收集
  • 原文地址:https://www.cnblogs.com/plateFace/p/6410375.html
Copyright © 2011-2022 走看看