mercredi 29 juin 2016

Using reflection methods that take array parameters efficiently

Currently I'm using some static arrays to avoid creating garbage for reflection methods that take arrays:

public static readonly object[]
    OneObject = new object[1],
    TwoObjects = new object[2];

Then I use them like:

public void Write(BinaryWriter writer, object value)
{
    Reflection.TwoObjects[0] = writer;
    Reflection.TwoObjects[1] = value;
    WriteMethod.Invoke(null, Reflection.TwoObjects);
}

Most of the time this will be fine, but it opens the door to multithreading bugs.

Is there a better way of doing this without allocating tiny arrays all over the place?





Aucun commentaire:

Enregistrer un commentaire