{"record":{"id":"7eb7bf45ec81bd70","repo":"MonoGame/MonoGame","slug":"vertexdata-does-not-inherit-ivertextype","errorCode":null,"errorMessage":"vertexData does not inherit IVertexType","messagePattern":"vertexData does not inherit IVertexType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/Vertices/VertexDeclaration.cs","lineNumber":218,"sourceCode":"        /// <returns>The VertexDeclaration.</returns>\n        /// <remarks>\n        /// Prefer to use VertexDeclarationCache when the declaration lookup\n        /// can be performed with a templated type.\n        /// </remarks>\n\t\tinternal static VertexDeclaration FromType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type vertexType)\n\t\t{\n\t\t\tif (vertexType == null)\n\t\t\t\tthrow new ArgumentNullException(\"vertexType\", \"Cannot be null\");\n\n            if (!ReflectionHelpers.IsValueType(vertexType))\n            {\n\t\t\t\tthrow new ArgumentException(\"Must be value type\", \"vertexType\");\n\t\t\t}\n\n            var type = Activator.CreateInstance(vertexType) as IVertexType;\n\t\t\tif (type == null)\n\t\t\t{\n\t\t\t\tthrow new ArgumentException(\"vertexData does not inherit IVertexType\");\n\t\t\t}\n\n            var vertexDeclaration = type.VertexDeclaration;\n\t\t\tif (vertexDeclaration == null)\n\t\t\t{\n\t\t\t\tthrow new Exception(\"VertexDeclaration cannot be null\");\n\t\t\t}\n\n\t\t\treturn vertexDeclaration;\n\t\t}\n\n        /// <summary>\n        /// Gets a copy of the vertex elements.\n        /// </summary>\n        /// <returns>A copy of the vertex elements.</returns>\n        public VertexElement[] GetVertexElements()\n\t\t{\n\t\t\treturn (VertexElement[])_data.Elements.Clone();","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/Vertices/VertexDeclaration.cs#L200-L236","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic struct MyVertex { public Vector3 Pos; }\nvar decl = VertexDeclaration.FromType(typeof(MyVertex));\n// after\npublic struct MyVertex : IVertexType {\n    public Vector3 Pos;\n    public VertexDeclaration VertexDeclaration => ...;\n}","handlingStrategy":"type-guard","validationCode":"if (!typeof(IVertexType).IsAssignableFrom(vertexType))\n    throw new ArgumentException($\"{vertexType} does not implement IVertexType.\");","typeGuard":"static bool ImplementsIVertexType(Type t) => typeof(IVertexType).IsAssignableFrom(t);","tryCatchPattern":"try { var decl = VertexDeclaration.FromType(vertexType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IVertexType\"))\n{ /* add : IVertexType to the struct and retry */ }","preventionTips":["Always declare custom vertex structs as ': IVertexType'.","Use built-in vertex types when possible.","Verify interface implementation before reflective lookup."],"tags":["vertexdeclaration","reflection","ivertextype","monogame","graphics"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}