{"record":{"id":"7cf9acd7c3340edd","repo":"dotnet/wpf","slug":"sr-unknownexpressionmode","errorCode":null,"errorMessage":"SR.UnknownExpressionMode","messagePattern":"SR\\.UnknownExpressionMode","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Expression.cs","lineNumber":110,"sourceCode":"                    break;\n\n                case ExpressionMode.NonSharable:\n                    _flags |= InternalFlags.NonShareable;\n                    break;\n\n                case ExpressionMode.ForwardsInvalidations:\n                    _flags |= InternalFlags.ForwardsInvalidations;\n                    _flags |= InternalFlags.NonShareable;\n                    break;\n\n                case ExpressionMode.SupportsUnboundSources:\n                    _flags |= InternalFlags.ForwardsInvalidations;\n                    _flags |= InternalFlags.NonShareable;\n                    _flags |= InternalFlags.SupportsUnboundSources;\n                    break;\n\n                default:\n                    throw new ArgumentException(SR.UnknownExpressionMode);\n            }\n        }\n\n\n        // We need this Clone method to copy a binding during Freezable.Copy.  We shouldn't be taking\n        // the target object/dp parameters here, but Binding.ProvideValue requires it.  (Binding\n        // could probably be re-factored so that we don't need this).\n        internal virtual Expression Copy( DependencyObject targetObject, DependencyProperty targetDP )\n        {\n            // By default, just use the same copy.\n            return this;\n        }\n\n\n        /// <summary>\n        ///     List of sources of the Expression\n        /// </summary>\n        /// <returns>Sources list</returns>","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Expression.cs#L92-L128","documentation":"Expression's constructor takes a mode controlling whether the expression is shareable, forwards invalidations, or supports unbound sources. The mode argument is validated against a known set of ExpressionMode values; any unknown/undefined mode reaches the default branch and throws ArgumentException(SR.UnknownExpressionMode). This is an internal-facing guard so bindings built with corrupted or undefined modes fail immediately.","triggerScenarios":"Constructing an Expression (e.g. via Binding internals or a subclass like ObjectRef/BindingExpression plumbing) passing an ExpressionMode value that is not one of the defined flag combinations, typically via an invalid cast of an int.","commonSituations":"Low-level WPF binding code or custom expression subclasses passing wrong enum constants; version mismatches where a mode constant changed between WindowsBase builds; tests exercising internal Expression constructors with bad flags.","solutions":["Pass a defined ExpressionMode constant (e.g. ExpressionMode.Shareable, ExpressionMode.ForwardsInvalidations, ExpressionMode.SupportsUnboundSources)","Validate with Enum.IsDefined before constructing","Check that the flag combination used is one the Expression constructor supports","Pin/align WindowsBase versions across the app to avoid changed enum values"],"exampleFix":"// before\nvar expr = new MyExpression((ExpressionMode)0x4000);\n// after\nvar expr = new MyExpression(ExpressionMode.ForwardsInvalidations);","handlingStrategy":"validation","validationCode":"static bool IsValidExpressionMode(ExpressionMode m) => m is ExpressionMode.None or ExpressionMode.Shareable or ExpressionMode.ForwardsInvalidations or ExpressionMode.SupportsUnboundSources;","typeGuard":"static bool IsDefinedEnum<TEnum>(object v) where TEnum : struct, Enum => Enum.IsDefined(typeof(TEnum), v);","tryCatchPattern":"try { var expr = new MyExpression(mode); }\ncatch (ArgumentException ex) { log.Error($\"Unknown expression mode: {ex.Message}\"); }","preventionTips":["Only combine documented ExpressionMode flags","Unit-test custom expression subclasses with every mode you plan to ship","Avoid casting ints to ExpressionMode"],"tags":["wpf","binding","invalid-argument-value","expression"],"backgroundTag":"invalid-enum-value","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"}