How can I achieve inheritance (or similar) with structs in C#? I know that an abstract struct isn't possible, but I need to achieve something similar.
I need it as a struct because it has to be a value type. And I need inheritance because I need a generic array and methods that I can garantee are there.
I think an example will go a long way, so heres some code that won't compile, but shows what I want to achieve;
abstract struct Vertex
{
abstract int SizeInBytes;
abstract void SetPointers();
}
struct ColorVertex : Vertex
{
Vector3 Position;
Vector4 Color;
override int SizeInBytes //static
{
get { return (3 + 4) * 4; }
}
override void SetVertexPointers() //static
{
...
}
}
class main
{
static void main()
{
Vertex[] verts = new Vertex[3];
for(int i = 0; i < 3; i++)
verts[i] = new ColorVertex();
verts[0].SetVertexPointers(); //should call ColorVertex.SetVertexPointers
externalAPIcall(verts);
}
}
EDIT:
The reason I need value types is so that I can make an array of them, and pass it to OpenGL as a vertex buffer. For this, the data needs to be directly contained in this array.
I'd be surprised if this were simply not possible to achieve.