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:
Rather than use reflection to access the internal array in a List<T>
, if you only need the ability to add, then I would actually recommend implementing your own resizable array (gasp!). It's not that hard.
Something like:
class ResizableArray<T>
{
T[] m_array;
int m_count;
public ResizableArray(int? initialCapacity = null)
{
m_array = new T[initialCapacity ?? 4]; // or whatever
}
internal T[] InternalArray { get { return m_array; } }
public int Count { get { return m_count; } }
public void Add(T element)
{
if (m_count == m_array.Length)
{
Array.Resize(ref m_array, m_array.Length * 2);
}
m_array[m_count++] = element;
}
}
Then you can get at the internal array with InternalArray
and know how many items are in the array using Count
.
You might be able to get a pointer out of a generic List, but I wouldn't recommend it and it probably wouldn't work the way you'd expect (if at all). Basically it means getting a pointer to an object, not a memory structure like an array.
I think you should go about this the other way around, and if you need speed then work directly on a byte array using structure array pointer in an unsafe context instead.
Background info:
"Even when used with the unsafe keyword, taking the address of a managed object, getting the size of a managed object, or declaring a pointer to a managed type is not allowed." - From C#: convert generic pointer to array
MSDN unsafe