Mimic C # array initializer behavior using Reflection

Using ILDasm to reverse engineer some C # code, I found out that the compiler uses the System.Runtime.CompilerServices.RuntimeHelper.InitializeArray method to initialize arrays that contain several constant values โ€‹โ€‹of primitive types.

It looks like it creates a special structure for each size of the array in bytes and marks it with the [StructLayout] . It is pretty simple. But then there is a field of this type that contains the actual data. How to create such a field in the form of Reflection.Emit and fill it with arbitrary bytes?

+4
source share
1 answer

I think you are using ModuleBuilder.DefineInitializedData for this.

+2
source

All Articles