(a) Foreword
When the two classes under different namespaces have the same attributes and need to be assigned to each other, the Jasen.Core.Info class in the following figure and the instance of the Jasen.Core.Test.Info class need to be assigned to each other, and the value is directly assigned according to the general idea. Typically, this situation is more common when invoking Web service. When you need to convert a lot of classes, or need to convert a lot of properties, we need to be based on certain rules to the design of this scenario, who will not be silly Bragi an attribute to the object to assign value.
(ii) The Objectmapper class is responsible for assigning values between the corresponding attributes of the object
<summary>//////</summary> public class Objectmapper {///<summary> </summary>///<param name= "sourcetype" ></param>///<param N Ame= "TargetType" ></param>///<returns></returns> public static Ilist<propertymapp Er> getmapperproperties (Type sourcetype, type TargetType) {var sourceproperties = Sourcetype.getpr
Operties ();
var targetproperties = targettype.getproperties (); Return (from S in Sourceproperties from T in targetproperties where s.name = = T.na Me && s.canread && t.canwrite && s.propertytype = = T.propertytype Select New P
Ropertymapper {sourceproperty = s,
TargetProperty = t }).
ToList (); }///<summary>//////</summary>///<param name= "source" ;</param>///<param name= "target" ></param> public static void Copyproperties (Object sou Rce, Object target) {var sourcetype = source.
GetType (); var targettype = target.
GetType ();
var mapperproperties = getmapperproperties (sourcetype, targettype); for (int index = 0,count=mapperproperties.count, index < count; index++) {var property = m
Apperproperties[index]; var Sourcevalue = property.
Sourceproperty.getvalue (source, NULL); Property.
Targetproperty.setvalue (target, sourcevalue, NULL); }
}
}