List to T[] without copying

后端 未结 8 754
名媛妹妹
名媛妹妹 2020-12-05 12:51

I\'m have a large list of value types that needs to be given to OpenGL. It would be great if this could happen as quickly as possible. What I\'m doing now looks like this:

8条回答
  •  情歌与酒
    2020-12-05 13:50

    You can do that with reflection:

    public static T[] GetUnderlyingArray(this List list)
    {
        var field = list.GetType().GetField("_items",
            System.Reflection.BindingFlags.Instance |
            System.Reflection.BindingFlags.NonPublic);
        return (T[])field.GetValue(list);
    }
    

    edit: ah someone already said it while I was testing this..

提交回复
热议问题