{"record":{"id":"ce3dd207cfd72f03","repo":"dotnet/wpf","slug":"sr-objectreaderinstancedescriptorincompatibleargumenttypes","errorCode":null,"errorMessage":"SR.ObjectReaderInstanceDescriptorIncompatibleArgumentTypes","messagePattern":"SR\\.ObjectReaderInstanceDescriptorIncompatibleArgumentTypes","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":1288,"sourceCode":"                if (arguments is not null)\n                {\n                    if (arguments.Count != methodParams.Length)\n                    {\n                        throw new XamlObjectReaderException(SR.ObjectReaderInstanceDescriptorIncompatibleArguments);\n                    }\n\n                    int argPos = 0;\n                    foreach (var argument in arguments)\n                    {\n                        var parameterInfo = methodParams[argPos++];\n                        if (argument is null)\n                        {\n                            if (parameterInfo.ParameterType.IsValueType)\n                            {\n                                if (!(parameterInfo.ParameterType.IsGenericType &&\n                                    parameterInfo.ParameterType.GetGenericTypeDefinition() == typeof(Nullable<>)))\n                                {\n                                    throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderInstanceDescriptorIncompatibleArgumentTypes, \"null\", parameterInfo.ParameterType));\n                                }\n                            }\n                        }\n                        else if (!parameterInfo.ParameterType.IsAssignableFrom(argument.GetType()))\n                        {\n                            throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderInstanceDescriptorIncompatibleArgumentTypes, argument.GetType(), parameterInfo.ParameterType));\n                        }\n                    }\n                }\n            }\n\n            private void AddArgumentsMembers(ICollection arguments, SerializerContext context)\n            {\n                if (arguments is not null && arguments.Count > 0)\n                {\n                    var itemsProperty = new MemberMarkupInfo\n                    {\n                        XamlNode = new XamlNode(XamlNodeType.StartMember, XamlLanguage.Items)","sourceCodeStart":1270,"sourceCodeEnd":1306,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L1270-L1306","documentation":"Each argument of the InstanceDescriptor is type-checked against the corresponding method parameter: a null argument is only allowed for reference types or Nullable<T>, and non-null arguments must be assignable to the parameter type. Violating either rule throws XamlObjectReaderException naming the argument type and the expected parameter type.","triggerScenarios":"Reading an instance whose InstanceDescriptor passes null for a non-nullable value-type parameter (XamlObjectReader.cs:1281-1290), or an argument whose runtime type is not assignable to the parameter type (XamlObjectReader.cs:1292-1295).","commonSituations":"Converters passing null for struct parameters (int, Guid, enums) after refactorings; passing a derived/base type or boxed incompatible type (e.g. double where int is expected); signature changes from int to Nullable<int> or vice versa leaving stale descriptor code.","solutions":["Replace null with a concrete value of the parameter type (or default(T)) when the parameter is a non-nullable value type.","Change the constructor/parameter to Nullable<T> or a reference type if a null argument is legitimately required.","Convert the argument to the exact parameter type before building the InstanceDescriptor (e.g. Convert.ChangeType or explicit cast)."],"exampleFix":"// before\nreturn new InstanceDescriptor(ctor, new object[] { null, name }); // param 0 is int\n// after\nreturn new InstanceDescriptor(ctor, new object[] { 0, name }); // supply valid int\n// or change ctor to accept int? for nullability","handlingStrategy":"validation","validationCode":"var pars = ((MethodBase)descriptor.MemberInfo).GetParameters();\nfor (int i = 0; i < pars.Length; i++) {\n  var arg = descriptor.Arguments[i];\n  if (arg is null && pars[i].ParameterType.IsValueType && Nullable.GetUnderlyingType(pars[i].ParameterType) is null)\n    throw new InvalidOperationException($\"Argument {i}: null not allowed for {pars[i].ParameterType}.\");\n  if (arg is not null && !pars[i].ParameterType.IsInstanceOfType(arg))\n    throw new InvalidOperationException($\"Argument {i} type mismatch: {arg.GetType()} vs {pars[i].ParameterType}.\");\n}","typeGuard":"bool ArgsMatchParams(InstanceDescriptor d) => d.MemberInfo is not MethodBase m || m.GetParameters().Zip(d.Arguments.Cast<object?>()).All(p => p.Second is null ? !(p.First.ParameterType.IsValueType && Nullable.GetUnderlyingType(p.First.ParameterType) is null) : p.First.ParameterType.IsInstanceOfType(p.Second));\n","tryCatchPattern":"try { using var r = new XamlObjectReader(instance); }\ncatch (XamlObjectReaderException ex) when (ex.Message.Contains(\"IncompatibleArgumentTypes\")) { /* coerce or null-fix arguments before rebuilding descriptor */ }","preventionTips":["Never pass null for value-type parameters in InstanceDescriptor arguments.","Match exact numeric types (int vs double) — no implicit conversions happen.","Update descriptors when parameters become Nullable<T> or change type."],"tags":["xaml","instancedescriptor","type-mismatch"],"backgroundTag":"type-mismatch","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"}