Now, service-oriented development has become an essential concept for b\s and c\s development, and has previously done a WCF service to invoke this service in the MVC Development mode on the Web side, which involves the replication of entity class data. Because the server-side model and the model in MVC are theoretically scoped differently, when you get the data returned by the service, you need to use the entity classes to load the data. In general, this is done by declaring a function on the Web side (no reflection or deep cloning required) to two parameters, one source object and one target object. Okay, here's the problem. Sometimes there are static fields in the entity class, so we have to deal with the data replication problem of this static field. The source code is as follows:
Private voidEntitytoentity<tsource, ttarget> (TSource s) whereTSource:New() whereTtarget:New() { if(T.gettype (). GetFields ()! =NULL) { foreach(varP2inchT.gettype (). GetFields ()) {varS1 = S.gettype (). GetField (P2. Name);//Find field information for the source entity classP2. SetValue (t, S1. GetValue (NULL)); } }
foreach(varPinchT.gettype (). GetProperties ())//target table as reference object { //Copy to local variable varP1 =p; { varS2 =S.gettype (). GetProperty (P1. Name); if(S2 = =NULL)Continue; P1. SetValue (t, S2. GetValue (s),NULL),NULL); } } }
A common entity-class data replication solution in service-oriented (interface) development process