{"record":{"id":"f40579ba1782026a","repo":"dotnet/machinelearning","slug":"cannot-set-parameter-param-name-for-obj-gettype","errorCode":null,"errorMessage":"Cannot set parameter {param.Name} for {obj.GetType()}","messagePattern":"Cannot set parameter (.+?) for (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.AutoML/TrainerExtensions/TrainerExtensionUtil.cs","lineNumber":296,"sourceCode":"                                fi.SetValue(obj, null);\n                            else if (fi.FieldType.IsEnum)\n                            {\n                                // Check if there is an enum option named Auto\n                                var enumDict = fi.FieldType.GetEnumValues().Cast<int>()\n                                    .ToDictionary(v => Enum.GetName(fi.FieldType, v), v => v);\n                                if (enumDict.ContainsKey(\"Auto\"))\n                                    fi.SetValue(obj, enumDict[\"Auto\"]);\n                            }\n                        }\n                        else\n                            SetValue(fi, (IComparable)dp.Options[optIndex], obj, propType);\n                    }\n                    else\n                        SetValue(fi, param.RawValue, obj, propType);\n                }\n                catch (Exception)\n                {\n                    throw new InvalidOperationException($\"Cannot set parameter {param.Name} for {obj.GetType()}\");\n                }\n            }\n        }\n\n        public static TrainerName GetTrainerName(BinaryClassificationTrainer binaryTrainer)\n        {\n            switch (binaryTrainer)\n            {\n                case BinaryClassificationTrainer.FastForest:\n                    return TrainerName.FastForestBinary;\n                case BinaryClassificationTrainer.FastTree:\n                    return TrainerName.FastTreeBinary;\n                case BinaryClassificationTrainer.LightGbm:\n                    return TrainerName.LightGbmBinary;\n                case BinaryClassificationTrainer.LbfgsLogisticRegression:\n                    return TrainerName.LbfgsLogisticRegressionBinary;\n                case BinaryClassificationTrainer.SdcaLogisticRegression:\n                    return TrainerName.SdcaLogisticRegressionBinary;","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.AutoML/TrainerExtensions/TrainerExtensionUtil.cs#L278-L314","documentation":"UpdateFields reflects over trainer option objects and sets fields/properties from raw parameter values; if reflection-based assignment fails (missing field, type mismatch, null value) it rethrows as InvalidOperationException naming the parameter and target type.","triggerScenarios":"CreateOptions/CreateLightGbmOptions building trainer options from a parameter set where a parameter name doesn't match an option field or its value type is incompatible (e.g. a bool parameter applied to an int field).","commonSituations":"Version drift between AutoML trainer-extension parameter names and the underlying trainer option classes; custom trainer registrations supplying wrong parameter names or raw value types.","solutions":["Check the parameter name matches the exact field/property on the options type.","Ensure RawValue's runtime type matches the option field type (cast/convert before adding to the ParameterSet).","Update Microsoft.ML.AutoML and trainer packages to matching versions.","Catch InvalidOperationException and log obj.GetType() plus param.Name to identify the mismatch."],"exampleFix":"// before\nparamSet.Add(new Parameter(\"NumLeaves\", \"32\")); // string into int field\n// after\nparamSet.Add(new Parameter(\"NumLeaves\", 32)); // correct type","handlingStrategy":"validation","validationCode":"var field = typeof(TOpts).GetField(param.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);\nif (field == null) throw new ArgumentException($\"Unknown option field {param.Name}\");\nif (!field.FieldType.IsInstanceOfType(param.RawValue)) throw new ArgumentException($\"Type mismatch for {param.Name}\");","typeGuard":null,"tryCatchPattern":"try { TrainerExtensionUtil.UpdateFields(options, paramSet); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"Failed applying parameter to {OptionsType}\", options.GetType()); throw; }","preventionTips":["Match parameter names exactly to option fields","Store RawValues in their declared types","Keep AutoML and trainer package versions in sync"],"tags":["csharp","reflection","options"],"backgroundTag":"type-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}