{"record":{"id":"27f7bba08155b2a6","repo":"unoplatform/uno","slug":"convertertype-0-is-not-derived-from-1-type","errorCode":null,"errorMessage":"ConverterType '{0}' is not derived from '{1}' type","messagePattern":"ConverterType '(.+?)' is not derived from '(.+?)' type","errorType":"exception","errorClass":"XamlSchemaException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlValueConverter.cs","lineNumber":96,"sourceCode":"\t\t\n\t\tpublic bool Equals (XamlValueConverter<TConverterBase> other)\n\t\t{\n\t\t\treturn !IsNull (other) && ConverterType == other.ConverterType && TargetType == other.TargetType && Name == other.Name;\n\t\t}\n\n\t\tpublic override bool Equals (object obj)\n\t\t{\n\t\t\tvar a = obj as XamlValueConverter<TConverterBase>;\n\t\t\treturn Equals (a);\n\t\t}\n\n\t\tprotected virtual TConverterBase CreateInstance ()\n\t\t{\n\t\t\tif (ConverterType == null)\n\t\t\t\treturn null;\n\n\t\t\tif (!typeof (TConverterBase).IsAssignableFrom (ConverterType))\n\t\t\t\tthrow new XamlSchemaException (String.Format (CultureInfo.InvariantCulture, \"ConverterType '{0}' is not derived from '{1}' type\", ConverterType, typeof (TConverterBase)));\n\n\t\t\tif (TargetType != null && TargetType.UnderlyingType != null) {\n\t\t\t\t// special case: Enum\n\t\t\t\tif (TargetType.UnderlyingType.IsEnum)\n\t\t\t\t\treturn (TConverterBase) (object) new EnumConverter (TargetType.UnderlyingType);\n\t\t\t\t// special case: Nullable<T>\n\t\t\t\tif (TargetType.IsNullable && TargetType.UnderlyingType.IsValueType)\n\t\t\t\t\treturn (TConverterBase) (object) new NullableConverter (TargetType.UnderlyingType);\n\t\t\t}\n\n\t\t\tif (ConverterType.GetConstructor (Type.EmptyTypes) == null)\n\t\t\t\treturn null;\n\n\t\t\treturn (TConverterBase) Activator.CreateInstance (ConverterType, true);\n\t\t}\n\n\t\tpublic override int GetHashCode ()\n\t\t{","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlValueConverter.cs#L78-L114","documentation":"Thrown by XamlValueConverter<TConverterBase>.CreateInstance (accessed via the ConverterInstance property) when the ConverterType is not assignable to the generic TConverterBase constraint. CreateInstance instantiates the converter, and the assignability check guards against a type mismatch that would otherwise cause an InvalidCastException during Activator.CreateInstance.","triggerScenarios":"Constructing an XamlValueConverter<ValueSerializer> with a converterType that derives from TypeConverter rather than ValueSerializer (or vice versa). Accessing ConverterInstance triggers the lazy CreateInstance.","commonSituations":"Schema-context code that pairs a converter type with the wrong TConverterBase generic parameter — e.g. assigning a TypeConverter-derived type to a ValueConverter-targeted XamlValueConverter. Incorrect custom XamlSchemaContext.GetXamlValueConverter overrides that return mismatched converter types.","solutions":["Verify the converterType passed to XamlValueConverter<T> actually derives from (or implements) TConverterBase before construction.","In custom XamlSchemaContext overrides, ensure GetXamlValueConverter returns converters whose Type matches the requested generic kind (TypeConverter vs ValueSerializer).","Add a type guard: typeof(TConverterBase).IsAssignableFrom(converterType) before creating the XamlValueConverter."],"exampleFix":"// before\nvar vc = new XamlValueConverter<ValueSerializer>(typeof(MyTypeConverter), targetType);\nvar inst = vc.ConverterInstance; // throws: MyTypeConverter is not a ValueSerializer\n\n// after\nif (typeof(ValueSerializer).IsAssignableFrom(typeof(MyValueSerializer)))\n    var vc = new XamlValueConverter<ValueSerializer>(typeof(MyValueSerializer), targetType);","handlingStrategy":"type-guard","validationCode":"if (converterType != null && !typeof(TConverterBase).IsAssignableFrom(converterType))\n    throw new ArgumentException(\"converterType mismatch\");\nvar vc = new XamlValueConverter<TConverterBase>(converterType, targetType, name);","typeGuard":"static bool IsCompatible<TBase>(Type converterType)\n    => converterType == null || typeof(TBase).IsAssignableFrom(converterType);","tryCatchPattern":"try { return vc.ConverterInstance; }\ncatch (XamlSchemaException ex) { /* log converter type mismatch */ return null; }","preventionTips":["Verify typeof(TConverterBase).IsAssignableFrom(converterType) before constructing.","Pair converter types with the correct generic parameter (TypeConverter vs ValueSerializer)."],"tags":["xaml","schema","type-mismatch","value-converter","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}