usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;namespacereflecttools{classProgram {Static voidMain (string[] args) {Person A=NewPerson {Name="Plate face No. 1th", Info=NewDetails {IsIT=false, Ismarriage=true, Code="Hello World that's impossible grass mud horse"}, PersonInfo=NewCodeworker {Describe="I'm a happy programmer." } }; Personvi VI= program.cloneclass<personvi>(a); Console.ReadLine (); } Public StaticT cloneclass<t> (Object sourceobj)whereT:class,New() {Type type=typeof(T); ObjectTargetobj =activator.createinstance (type); Type TargetType=Targetobj.gettype (); Type sourcetype=Sourceobj.gettype (); Clonework (SourceType, TargetType, Sourceobj, targetobj); return(T) targetobj; } Public Static voidClonework (Type sourcetype, type TargetType,ObjectSourceobj,Objecttargetobj) {fieldinfo[] Info= Sourcetype.getfields (BindingFlags.Public | BindingFlags.NonPublic |bindingflags.instance); FieldInfo Targetinfo=NULL; Objectval =NULL; for(inti =0; I < info. Length; i++) {Targetinfo=Targettype.getfield (Info[i]. Name); if(Targetinfo = =NULL) {Console.WriteLine (Info[i]. Name+"didn't find"); 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 (targetin Fo. FieldType)); } //description is an ordinary classclonework (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 classPerson { Public stringName; PublicDetails Info; PublicCodeworker PersonInfo; } Public classDetails { Public BOOLIsmarriage; Public BOOLIsIT; PublicString Code; } Public classCodeworker { PublicString Describe; } Public classPersonvi { Public stringName; PublicDetails Info; PublicCodeworker PersonInfo; }}
C # leverages reflective copy classes