Any two objects have the same field assigned values, including common property, other class objects, and parent class property. Unknown type because it is any object
Therefore, the general practice is to directly call reflection. spring.net encapsulates the method for assigning values to any object. However, after my test, I found that it is slower than direct reflection.
Or is there a problem with the method execution? After a rough look at the internal source code of spring.net, we found that it implemented the Expression Tree and AST to parse its packaging object,
I don't know much about it. If anyone has studied it before, I can share some technical articles.
In this example, we use direct reflection. spring.net's objectwrapper and jeffreyzhao's fastreflectionlib http://www.codeplex.com/fastreflectionlibto enable the value.
Efficiency Comparison.
The direct reflection code is as follows:
Direct code reflection
/// <Summary>
/// Assign the value of the original object to the target object
/// </Summary>
/// <Param name = "originobj"> original object </param>
/// <Param name = "targetobj"> target object </param>
Public static void fill (Object originobj, object targetobj)
{
Propertyinfo [] properties = originobj. GetType (). getproperties ();
Foreach (propertyinfo item in properties)
{
Propertyinfo property = targetobj. GetType (). getproperty (item. Name );
Try
{
Property. setvalue (targetobj, item. getvalue (originobj, null), null );
}
Catch
{
Continue;
}
}
}
Spring.net implements the assignment code using objectwrapper as follows:
Spring.net
/// <Summary>
/// Assign the value of the original object to the target object
/// </Summary>
/// <Param name = "originobj"> original object </param>
/// <Param name = "targetobj"> target object </param>
Public static void fill <O, T> (Object originobj, object targetobj)
{
Objectwrapper ow = new objectwrapper (originobj );
Objectwrapper Tw = new objectwrapper (targetobj );
Propertyinfo [] properties = ow. getpropertyinfos ();
Foreach (propertyinfo item in properties)
{
Try
{
Tw. setpropertyvalue (item. Name, ow. getpropertyvalue (item. Name ));
}
Catch
{
Continue;
}
}
}
The assignment code using fastreflectionlib is as follows:
Fastreflectionlib
/// <Summary>
/// Assign the value of the original object to the target object
/// </Summary>
/// <Param name = "originobj"> </param>
/// <Param name = "targetobj"> </param>
Public static void fill (Object originobj, object targetobj)
{
Propertyinfo [] properties = originobj. GetType (). getproperties ();
Foreach (propertyinfo item in properties)
{
Propertyinfo property = targetobj. GetType (). getproperty (item. Name );
Try
{
Property. fastsetvalue (targetobj, item. fastgetvalue (originobj ));
}
Catch
{
Continue;
}
}
}
To make any object more authentic, my two objects will inherit one class, and other objects will be included in the object, the specific content of the object will not be displayed, the following provides the source generation
Download Code. If you are interested, you can download it. If you have any good suggestions, please do not forget to tell me.
The test code is as follows:
Test code
Static void main (string [] ARGs)
{
VaR origin = new sourceobject () {id = 2, name = "Lawson", url = "http://lawson.cnblogs.com", createtime = new datetime (2000, 10, 10 ), basestr = "1 base", othero = new otherobj () {others = "Other Object others "}};
VaR target = new targetobject () {id = 3, name = "AA", url = "http://www.cnblogs.com", createtime = new datetime (2010, 10, 1 )};
Int time = 10000;
Stopwatch stopwatmethane = stopwatch. startnew ();
For (INT I = 0; I <time; I ++)
{
Target. ID = origin. ID;
Target. Name = origin. Name;
Target. url = origin. url;
Target. createtime = origin. createtime;
}
Console. writeline (string. Format ("Time consumed by direct Value assignment: {0} millisecond", stopwatch4.elapsedmilliseconds ));
Stopwatch stopwatch2 = stopwatch. startnew ();
For (INT I = 0; I <time; I ++)
{
Directreflect. exchanger. Fill (origin, target );
}
Console. writeline (string. Format ("Time consumed for direct reflection assignment: {0} millisecond", stopwatch2.elapsedmilliseconds ));
Stopwatch stopwatch = stopwatch. startnew ();
For (INT I = 0; I <time; I ++)
{
Springnet. exchanger. Fill <sourceobject, targetobject> (origin, target );
}
Console. writeline (string. Format ("springnet value elapsed time: {0} millisecond", stopwatch. elapsedmilliseconds ));
Stopwatch stopwatch3 = stopwatch. startnew ();
For (INT I = 0; I <time; I ++)
{
Fastreflect. exchanger. Fill (origin, target );
}
Console. writeline (string. Format ("fastreflect Assignment time: {0} millisecond", stopwatch3.elapsedmilliseconds ));
Console. readkey ();
}
The result of release compilation and running is as follows:
Time consumed for direct assignment: 0 ms
Time consumed for direct reflection assignment: 187 milliseconds
Springnet value takes 3642 milliseconds
Fastreflect value takes 133 milliseconds
In my tests, springnet compared objectwrapper with direct reflection or fastreflect, the performance was not an order of magnitude at all.
Download my test code below
/Files/Lawson/ExchangeValue-lawson.rar