{"record":{"id":"207e57905a0b5c7e","repo":"OrchardCMS/OrchardCore","slug":"a-property-0-exists-but-is-not-decorated-with-1","errorCode":null,"errorMessage":"A property \"{0}\" exists but is not decorated with \"{1}\"","messagePattern":"A property \"(.+?)\" exists but is not decorated with \"(.+?)\"","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs","lineNumber":44,"sourceCode":"        if (context.Switches != null && context.Switches.Count > 0)\n        {\n            foreach (var commandSwitch in context.Switches)\n            {\n                SetSwitchValue(commandSwitch);\n            }\n        }\n    }\n\n    private void SetSwitchValue(KeyValuePair<string, string> commandSwitch)\n    {\n        // Find the property.\n        var propertyInfo = GetType()\n            .GetProperty(commandSwitch.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase)\n            ?? throw new InvalidOperationException(S[\"Switch \\\"{0}\\\" was not found\", commandSwitch.Key]);\n\n        if (propertyInfo.GetCustomAttributes(typeof(OrchardSwitchAttribute), false).Length == 0)\n        {\n            throw new InvalidOperationException(S[\"A property \\\"{0}\\\" exists but is not decorated with \\\"{1}\\\"\", commandSwitch.Key, nameof(OrchardSwitchAttribute)]);\n        }\n\n        // Set the value.\n        try\n        {\n            var value = ConvertToType(propertyInfo.PropertyType, commandSwitch.Value);\n            propertyInfo.SetValue(this, value, null /*index*/);\n        }\n        catch (Exception ex) when (!ex.IsFatal())\n        {\n            // TODO: (ngm) fix this message.\n            var message = S[\"Error converting value \\\"{0}\\\" to \\\"{1}\\\" for switch \\\"{2}\\\"\",\n                commandSwitch.Value,\n                propertyInfo.PropertyType.FullName,\n                commandSwitch.Key];\n\n            throw new InvalidOperationException(message, ex);\n        }","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs#L26-L62","documentation":"A switch name matched a public property on the command handler, but that property lacks the required [OrchardSwitch] attribute, so it is not an eligible command switch. OrchardCore requires the attribute as an explicit opt-in; otherwise arbitrary public properties (like Context) could be set from the CLI. The throw happens right after the reflection lookup in SetSwitchValue.","triggerScenarios":"Calling SetSwitchValues with a switch whose key equals a plain public property name on the handler that was never decorated with [OrchardSwitchAttribute], e.g. `mycommand -Context foo`.","commonSituations":"Adding a new handler property but forgetting the attribute, or intentionally matching an infrastructure property by name from the command line.","solutions":["Decorate the intended property with [OrchardSwitch] so it becomes a valid switch.","Remove the offending switch from the command invocation if it was not meant to be settable.","Verify the attribute is the Orchard-specific OrchardSwitchAttribute and not a similarly named attribute that does not match the typeof check."],"exampleFix":"// before\npublic class MyCommand {\n  public bool Verbose { get; set; }\n}\n// after\npublic class MyCommand {\n  [OrchardSwitch]\n  public bool Verbose { get; set; }\n}","handlingStrategy":"validation","validationCode":"var prop = handlerType.GetProperty(switchName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);\nif (prop?.GetCustomAttributes(typeof(OrchardSwitchAttribute), false).Length == 0)\n    throw new InvalidOperationException($\"Property '{switchName}' is not a valid switch (missing [OrchardSwitch])\");","typeGuard":null,"tryCatchPattern":"try { await handler.ExecuteAsync(context); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not decorated\")) { logger.LogError(\"Switch targets a non-switch property; add [OrchardSwitch] or remove the switch\"); }","preventionTips":["Every public switch property must carry [OrchardSwitch]","Review new handler properties in code review to ensure they are either attributed or renamed to avoid collision with switch names"],"tags":["commands","reflection","attributes","cli"],"backgroundTag":"schema-validation-failed","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}