MonoGame/MonoGame · error · ArgumentException
vertexData does not inherit IVertexType
Error message
vertexData does not inherit IVertexType
What it means
Thrown by VertexDeclaration.FromType when the instantiated vertexType object does not implement IVertexType. FromType uses Activator.CreateInstance to build an instance and casts to IVertexType to read VertexDeclaration; if the cast fails (type does not implement the interface), this ArgumentException is thrown.
Source
Thrown at MonoGame.Framework/Graphics/Vertices/VertexDeclaration.cs:218
/// <returns>The VertexDeclaration.</returns>
/// <remarks>
/// Prefer to use VertexDeclarationCache when the declaration lookup
/// can be performed with a templated type.
/// </remarks>
internal static VertexDeclaration FromType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type vertexType)
{
if (vertexType == null)
throw new ArgumentNullException("vertexType", "Cannot be null");
if (!ReflectionHelpers.IsValueType(vertexType))
{
throw new ArgumentException("Must be value type", "vertexType");
}
var type = Activator.CreateInstance(vertexType) as IVertexType;
if (type == null)
{
throw new ArgumentException("vertexData does not inherit IVertexType");
}
var vertexDeclaration = type.VertexDeclaration;
if (vertexDeclaration == null)
{
throw new Exception("VertexDeclaration cannot be null");
}
return vertexDeclaration;
}
/// <summary>
/// Gets a copy of the vertex elements.
/// </summary>
/// <returns>A copy of the vertex elements.</returns>
public VertexElement[] GetVertexElements()
{
return (VertexElement[])_data.Elements.Clone();View on GitHub (pinned to 1d71bbd0ff)
Solutions
- Implement IVertexType on the vertex struct (provide a public VertexDeclaration property returning the layout).
- Use a built-in vertex type from Microsoft.Xna.Framework.Graphics (e.g., VertexPositionColor) which already implements IVertexType.
- Verify the type implements IVertexType via typeof(IVertexType).IsAssignableFrom(vertexType) before calling.
Example fix
// before
public struct MyVertex { public Vector3 Pos; }
var decl = VertexDeclaration.FromType(typeof(MyVertex));
// after
public struct MyVertex : IVertexType {
public Vector3 Pos;
public VertexDeclaration VertexDeclaration => ...;
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!typeof(IVertexType).IsAssignableFrom(vertexType))
throw new ArgumentException($"{vertexType} does not implement IVertexType."); Type guard
static bool ImplementsIVertexType(Type t) => typeof(IVertexType).IsAssignableFrom(t);
Try / catch
try { var decl = VertexDeclaration.FromType(vertexType); }
catch (ArgumentException ex) when (ex.Message.Contains("IVertexType"))
{ /* add : IVertexType to the struct and retry */ } Prevention
- Always declare custom vertex structs as ': IVertexType'.
- Use built-in vertex types when possible.
- Verify interface implementation before reflective lookup.
When it happens
Trigger: Passing a struct Type that does not implement IVertexType (no public VertexDeclaration property via the interface). The struct exists and is a value type but lacks the required interface.
Common situations: Defining a custom vertex struct and forgetting ' : IVertexType', or passing a built-in type that is not a vertex type. Note: the parameter name in the message ('vertexData') is a minor doc inconsistency but the cause is the type argument.
Related errors
- VertexDeclaration cannot be null
- Cannot be null
- Must be value type
- Elements cannot be empty
- Type '{newBitmapType}' is not a subclass of BitmapContent.
AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13).
Data as JSON: /api/errors/7eb7bf45ec81bd70.
Report an issue: GitHub.