{"record":{"id":"cf80007a1865ba2a","repo":"dotnet/wpf","slug":"sr-format-sr-freezable-cloneinvalidtype-typeof-t-name","errorCode":null,"errorMessage":"SR.Format(SR.Freezable_CloneInvalidType, typeof(T).Name)","messagePattern":"SR\\.Format\\(SR\\.Freezable_CloneInvalidType, typeof\\(T\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs","lineNumber":826,"sourceCode":"                        newValue = itemAsFreezable.Clone() as T;\n                        break;\n                    case CloneCommonType.CloneCurrentValue:\n                        newValue = itemAsFreezable.CloneCurrentValue() as T;\n                        break;\n                    case CloneCommonType.GetAsFrozen:\n                        newValue = itemAsFreezable.GetAsFrozen() as T;\n                        break;\n                    case CloneCommonType.GetCurrentValueAsFrozen:\n                        newValue = itemAsFreezable.GetCurrentValueAsFrozen() as T;\n                        break;\n                    default:\n                        Invariant.Assert(false, \"Invalid CloneCommonType encountered.\");\n                        break;\n                    }\n\n                    if (newValue == null)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.Freezable_CloneInvalidType, typeof(T).Name));\n                    }\n                }\n\n                OnFreezablePropertyChanged(oldValue: null, newValue);\n                _collection.Add(newValue);\n            }\n        }\n\n        /// <summary>\n        /// Implementation of Freezable.CloneCore()\n        /// </summary>\n        protected override void CloneCore(Freezable source)\n        {\n            base.CloneCore(source);\n\n            FreezableCollection<T> sourceFreezableCollection = (FreezableCollection<T>) source;\n\n            CloneCommon(sourceFreezableCollection, CloneCommonType.Clone);","sourceCodeStart":808,"sourceCodeEnd":844,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L808-L844","documentation":"CloneCommon throws InvalidOperationException(SR.Freezable_CloneInvalidType) when cloning a FreezableCollection whose element type T cannot be cloned: the item is neither Freezable nor ICloneable (or otherwise not handled by the CloneCommonType switch), so the cloned item newValue remains null. It indicates T is a type the clone machinery cannot deep-copy.","triggerScenarios":"Calling Clone/CloneCore/CloneCurrentValueCore/GetAsFrozen/GetCurrentValueAsFrozen on a FreezableCollection<T> where T is not Freezable and lacks a supported clone path, e.g. FreezableCollection<SomePoco> or a custom type not deriving from Freezable.","commonSituations":"Declaring FreezableCollection<T> with a plain business object for convenience, then freezing/cloning the collection for animation or thread marshalling; SDK retargeting where a custom element type lost its Freezable base.","solutions":["Make T a Freezable-derived type (override CloneCore/CloneCurrentValueCore) so the clone machinery can copy elements.","Implement cloning support for T (Freezable) instead of a plain class.","Perform a manual copy (foreach item, create and populate a new instance) rather than calling Clone."],"exampleFix":"// before\nclass MyItem { public int X; }\nvar col = new FreezableCollection<MyItem>();\nvar copy = col.Clone(); // throws\n// after\nclass MyItem : Freezable {\n  public int X { get; set; }\n  protected override Freezable CreateInstanceCore() => new MyItem();\n  protected override void CloneCore(Freezable source) { X = ((MyItem)source).X; }\n}","handlingStrategy":"validation","validationCode":"bool cloneable = typeof(Freezable).IsAssignableFrom(typeof(T)) || typeof(ICloneable).IsAssignableFrom(typeof(T));\nif (!cloneable) throw new InvalidOperationException($\"{typeof(T).Name} cannot be cloned; derive it from Freezable.\");","typeGuard":"static bool IsCloneableElement<T>() => typeof(Freezable).IsAssignableFrom(typeof(T)) || typeof(ICloneable).IsAssignableFrom(typeof(T));","tryCatchPattern":"try { var copy = collection.Clone(); }\ncatch (InvalidOperationException ex) { /* fall back to manual copy of elements */ }","preventionTips":["Constrain FreezableCollection<T> element types to Freezable subclasses","Implement CreateInstanceCore and CloneCore on custom element types","Test Clone/GetAsFrozen on every collection type you ship"],"tags":["wpf","freezable","clone","type-constraint"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}