{"record":{"id":"86c898ac9ec8c64b","repo":"stride3d/stride","slug":"cannot-serialize-an-object-of-type-parttype-name-as-an-asset","errorCode":null,"errorMessage":"Cannot serialize an object of type {partType.Name} as an asset part reference: the type does not implement {typeof(IIdentifiable).Name}","messagePattern":"Cannot serialize an object of type (.+?) as an asset part reference: the type does not implement (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Serializers/IdentifiableAssetPartReference.cs","lineNumber":44,"sourceCode":"    public override string ToString()\n    {\n        return $\"{{AssetPartReference: {Id}}}\";\n    }\n\n    /// <inheritdoc/>\n    public void FillFromPart(object assetPart)\n    {\n        if (assetPart is not IIdentifiable identifiable)\n            throw new InvalidOperationException($\"Cannot serialize an object of type {assetPart.GetType().Name} as an asset part reference: the type does not implement {typeof(IIdentifiable).Name}\");\n\n        Id = identifiable?.Id ?? Guid.Empty;\n    }\n\n    /// <inheritdoc/>\n    public object? GenerateProxyPart(Type partType)\n    {\n        if (!typeof(IIdentifiable).IsAssignableFrom(partType))\n            throw new InvalidOperationException($\"Cannot serialize an object of type {partType.Name} as an asset part reference: the type does not implement {typeof(IIdentifiable).Name}\");\n\n        if (Id == Guid.Empty)\n            return null;\n\n        var assetPart = (IIdentifiable)Activator.CreateInstance(partType)!;\n        assetPart.Id = Id;\n        return assetPart;\n    }\n}\n","sourceCodeStart":26,"sourceCodeEnd":54,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Serializers/IdentifiableAssetPartReference.cs#L26-L54","documentation":"IdentifiableAssetPartReference.GenerateProxyPart materializes a proxy instance of the given part type and assigns the stored Id to it. It requires the type to implement IIdentifiable; otherwise the generated instance cannot receive the Id and InvalidOperationException is thrown. Like FillFromPart, this enforces the identifiable contract on both ends of the reference round-trip.","triggerScenarios":"Calling GenerateProxyPart(typeof(T)) where typeof(T) does not implement IIdentifiable — e.g. registering a reference for a part type that is not identifiable, or passing the wrong Type during proxy generation of a deserialized asset.","commonSituations":"Custom asset part types missing the IIdentifiable implementation on the deserialization side; refactorings that changed a part's base class; mismatched part types between the reference descriptor and the actual asset model.","solutions":["Make the part type implement IIdentifiable (derive from Identifiable) so GenerateProxyPart can assign the Id.","Pass the correct part Type — the identifiable one associated with the reference — to GenerateProxyPart.","Check typeof(IIdentifiable).IsAssignableFrom(partType) before calling and handle non-identifiable types separately.","Return null gracefully by ensuring Id == Guid.Empty only for intentionally empty references; fix the type for real parts."],"exampleFix":"// before\nvar part = (AssetPart)reference.GenerateProxyPart(typeof(PlainPart));\n\n// after\nclass PlainPart : Identifiable { ... } // or use the identifiable part type\nvar part = (AssetPart)reference.GenerateProxyPart(typeof(IdentifiablePart));","handlingStrategy":"type-guard","validationCode":"if (!typeof(IIdentifiable).IsAssignableFrom(partType))\n    throw new ArgumentException($\"{partType.Name} must implement IIdentifiable\", nameof(partType));","typeGuard":"bool IsIdentifiablePartType(Type t) => typeof(IIdentifiable).IsAssignableFrom(t);","tryCatchPattern":"try { return reference.GenerateProxyPart(partType); }\ncatch (InvalidOperationException) { log.Error($\"Cannot proxy non-identifiable type {partType.Name}\"); return null; }","preventionTips":["Ensure every part type used in references implements IIdentifiable","Check IsAssignableFrom before proxy generation","Keep part base classes stable across refactorings","Test deserialization of assets containing custom part types"],"tags":["serialization","proxy","identifiable","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}