samedi 10 mars 2018

Modifying Array Length property through Reflection

So, I am stuck with one weird problem.

To give you idea what I want to achieve, but failed misserably:

int[] testArray = new int[100];

typeof(int[])
.GetProperty("LongLength", BindingFlags.Instance | BindingFlags.Public)
.SetValue(testArray, 50);

Yeah, that is, I want to modify C# arrays Length property (only make it less than actual size), WITHOUT making new array and copying contents - to avoid GC etc etc...

Background:

In Unity, there is this function:

void SetIndices(int[] indices, MeshTopology topology, int submesh, bool calculateBounds);

As you see, SetIndices doesn't accept "start" and "end" or at least "length" as a parameter. This is why I want to modify Length of my indices array to trick this function.

Any ideas?





Aucun commentaire:

Enregistrer un commentaire