lundi 21 septembre 2020

Get array of list and keep reference?

For some performance reason , I want to get array of List directly in C#

https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,2765070d40f47b98

My code here:

private UIVertex[] GetArray(List<UIVertex> verts)
    {
        var aryF = typeof(List<UIVertex>).GetField("_items", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        return aryF.GetValue(verts) as UIVertex[];
    }

static List<UIVertex> verts
static UIVertex[] ary;
//...
ary = GetArray(verts);

In my case I can reuse 'verts' , But when 'verts' count change. my ary need to get again ( and it call reflection ) ,

reflection cost is too height , Can I just keep ary reference ? How to do it?





Aucun commentaire:

Enregistrer un commentaire