Monday, July 17, 2006

.Net Cloning

Two interesting articles on cloning:

http://www.codeproject.com/csharp/cloneimpl_class.asp?forumid=13965&exp=0&select=377851&df=100&fr=21.5#xx377851xx

http://www.agiledeveloper.com/articles/cloning072002.htm

The first one uses Reflection - main advantage is you don't have to update the function if you add fields to t he class - main disadvantage is you don't have detailed control (e.g. - if a field supports ICloneable it uses the clone mehtod, but Clone does a shallow copy - what if you want to do a deep copy - or what if you want to do some custom action - like generate a new guid for a guid field? - you can add customizations but then the function is not generic and you lose the main advantage.

The main disadvantge of the second one is that you have to modify it with any changes to the fields - but it is elegant and simple and for hte problem I'm working on right now, I have a lot of custom handling for different fields, so I don't think the first one would work.

Serialization is another option, but this gives a security issue - also may be a performance issue? and I would still have to write additional code for my custom handling.

No comments: