lundi 18 mai 2015

Copy / Reflection.Emit'ting static arrays

I'm attempting to copy a static initializers from one DLL to another.

If you have a static array initializer in C#, you get something like this:

.class private auto ansi <PrivateImplementationDetails>{0D3E8B0E-F218-435F-989D-9D04F550A786}
    extends [mscorlib]System.Object
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
    .field assembly static valuetype <PrivateImplementationDetails>{0D3E8B0E-F218-435F-989D-9D04F550A786}/__StaticArrayInitTypeSize=120 $$method0x6000060-1 = ((binary data))

I found the easiest way to read this data to use this:

int size = field.FieldType.StructLayoutAttribute.Size;
byte[] data = new byte[size];
RuntimeHelpers.InitializeArray(data, field.FieldHandle);

Basically this will give you the 'binary data' that's mentioned above.

Question 1: What will happen with Dictionary's? Will this still work? I'm having trouble figuring out what exactly happens here (it seems to be hidden from decompilation)...

I've already found that the GUID to use in the implementation details is the version ID in the ModuleBuilder. Using the other explicit field information that means you should be able to copy the data.

Question 2: How can you write the data back in another ModuleBuilder / FieldBuilder using Reflection.Emit ?





Aucun commentaire:

Enregistrer un commentaire