Friday, July 11, 2008 2:45 PM
A .NET Assembly for Cloning Objects with Arbitrary Field Value Changes: IcManipluator
After my last post, A Safe and Asynchronous One to Many Stream Copy Through IL and Inheritance”, I ordered a few books and spent some time playing with generating IL. Along the way I’ve developed a library which allows you to make a franken-clone of any object. You pass the method an object to clone along with a hash table of values to change, it returns a new object just as ordered.
The IcManipulator is based on Whizzo's Object Cloning IL Code. Instead of just blindly copying fields my code grabs values out of a hash table and places them in lieu of the old values. In this way private fields in a class can be altered.
I haven’t thoroughly tested the IcManipulator. It's a toy I’ve been using to learn about emitting IL and should under no circumstances be used in a production environment. Neither Atalasoft nor I take any responsibility for any real or perceived damage that may come about from use of this library.
IcManipulator is not complete. I am going to the IPCV’08 Conference next week and wanted to get this out there before I was neck deep in image processing.
The current release of the IcManipluator has a lot of limitations:
- 64-bit fields are broken. This means no Int64s, UInt64s and no Doubles. I’m not sure why; the code is commented out if you want to play with it.
- Only basic IL types are supported. This means Decimals, Objects and Strings cannot be changed. In a future version I hope to implement full object support.
- It will not work currently without full trust. However, it may be possible to use it in a partial trust sandbox as described in the MSDN Walkthrough: Emitting Code in Partial Trust Scenarios.
Using the IcManipluator library is very easy. For example, to make a read only clone of a MemoryStream:
Hashtable ht = new Hashtable();
MemoryStream ms2 =
The easiest way to find a list of internal fields in a class is to use Lutz Roeder's .NET Reflector. Simply open the assembly with the Reflector and inspect the internal variables. Often their function can be inferred directly from their name but if you need to you can dig in with the Reflector's disassembler.
Oh and if you hadn’t guessed, the name is a combination of horrible nerd jokes. The Icy Manipulator is a M:TG card and IC stands for Intrusion Countermeasure as in Intrusion Countermeasure Electronics or ICE as made famous by William Gibson.
Now, off to Las Vegas...
You Can Download Alpha 1 of the IcManipulator Here.