This subject is inspired on a session I followed on the TechDays 2008,
that addressed the fact that IL (Intermediate Language) can be used to clone objects, among other things, and that it's not evil at all, and it can be pretty performant also.
You only have to see that you don't overuse it tho, because otherwise the readability of your code is reduced, which is not a good thing for the maintenance.
And wrong usage of reflection (what IL is, or at least uses) can also result in poor performance.
This being said, I tested this on my own, with some self written code and comments,
just to test that it was really true what Mr. Roy Osherove told me ;)
Below you can see a screenshot of a console application that does the tests.
As you see, sometimes the IL code is even faster the the normal cloning on a simple class Person with a couple of fields in it that are filled in with some random values.
Let's take a look at the code, first we declare a simple class Person
I've added comments in the code so that people that don't understand IL too much, can also understand what's happening.
Person class definition:
The code below is a nice piece of code, read through the comments and you'll understand what's stated.
The basic thing that it does is, create a DynamicMethod, get the ILGenerator, emit code in the method, compile it to a delegate, and execute the delegate.
The delegate is cached so that the IL is not generated each time a cloning should take place, so we loose only one time performance, when the first object is cloned (the IL has to be created and compiled at runtime).
Hopefully this article is of use for some people, if so, let me know.