Initializing a large constant byte array field

111 views
Skip to first unread message

Azengar

unread,
Apr 1, 2019, 1:05:27 PM4/1/19
to mono-cecil
I've been struggling with this problem for a while. What I'm trying to do is generate a rather large array of constant bytes using mono-cecil
and I understand that I cannot use the FieldDefinition.Constant and FieldDefinition.InitialValue fields because the byte[] type is not a value type,
but I've peeked at some .net code with ildasm and it seems to do the following:

.class private auto ansi sealed '<PrivateImplementationDetails>'
       extends [mscorlib]System.Object
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
  .field static assembly initonly int32 BD346A9E43DFFF4439275DD65DC7D20506F6FDD9 at I_00003E84
} // end of class '<PrivateImplementationDetails>'


// =============================================================

.data cil I_00003E84 = bytearray (
                 00 15 21 06)    

Basically it creates a custom class containing a field with a reference to some .data that contains the constant bytes and then use this field every time this particular sequence of bytes is used.

I would like to imitate this behavior, how should I go about it? More specifically, how could I create some bytes in the .data section and then reference them as the value of my field.
Thanks to anyone taking the time to look at this, and if this has already been answered I apologize.

Jb Evain

unread,
Apr 4, 2019, 2:17:14 PM4/4/19
to mono-...@googlegroups.com
Hi,

That should be pretty easy. A FieldDefinition has a byte[] InitialValue property that you need to set.

Jb

--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups "mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mono-cecil+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages