dotnet/wpf · error · InvalidOperationException

SR.ChangingTypeNotAllowed

Error message

SR.ChangingTypeNotAllowed

What it means

ComponentResourceKey.TypeInTargetAssembly is a once-only init-only style property: once it has been set (or set via the constructor), assigning it again throws InvalidOperationException. WPF enforces this because the key is typically used in resource dictionaries and changing the type after use would corrupt dictionary lookups.

Solutions

  1. Create a new ComponentResourceKey instead of mutating the existing one's TypeInTargetAssembly
  2. Set TypeInTargetAssembly only once, at construction time via the (Type, object) constructor
  3. Check the _typeInTargetAssemblyInitialized state (or track it yourself) before assigning

Example fix

// before
key.TypeInTargetAssembly = typeof(Other); // throws after already set
// after
var newKey = new ComponentResourceKey(typeof(Other), key.ResourceId);
Defensive patterns

Strategy: validation

Validate before calling

if (!isKeyInitialized(key)) { key.TypeInTargetAssembly = targetType; } // track init yourself, or just construct a new key

Type guard

bool IsKeyInitialized(ComponentResourceKey k) => !ReferenceEquals(k, null) && k.ResourceId != null; // approximation; prefer constructing new keys

Try / catch

try { key.TypeInTargetAssembly = t; } catch (InvalidOperationException ex) { /* key already initialized: create new key */ }

Prevention

When it happens

Trigger: Assigning ComponentResourceKey.TypeInTargetAssembly a second time on the same instance after it was already set in the constructor or a previous assignment.

Common situations: Reusing a cached ComponentResourceKey instance across themes or assemblies and trying to retarget its TypeInTargetAssembly; initializing a shared key in XAML and then overwriting it in code-behind.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9d641e35dd35e2ed. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ComponentResourceKey.cs:54

            _resourceIdInitialized = true;
        }

        /// <summary>
        ///     The Type associated with this resources. Must be in assembly where the resource is located.
        /// </summary>
        public Type TypeInTargetAssembly
        {
            get
            {
                return _typeInTargetAssembly;
            }

            set
            {
                ArgumentNullException.ThrowIfNull(value);
                if (_typeInTargetAssemblyInitialized)
                {
                    throw new InvalidOperationException(SR.ChangingTypeNotAllowed);
                }
                _typeInTargetAssembly = value;
                _typeInTargetAssemblyInitialized = true;
            }
        }

        /// <summary>
        ///     Used to determine where to look for the resource dictionary that holds this resource.
        /// </summary>
        public override Assembly Assembly
        {
            get
            {
                return _typeInTargetAssembly?.Assembly;
            }
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)