dotnet/wpf · error · InvalidOperationException

SR.MarkupExtensionStaticMember

Error message

SR.MarkupExtensionStaticMember

What it means

StaticExtension.ProvideValue throws InvalidOperationException (SR.MarkupExtensionStaticMember) when the internal _member field is null. This occurs when the extension was built with the parameterless constructor and neither Member nor MemberType was ever assigned. ProvideValue needs the member (or a MemberType + member string) to locate the static field/property value.

Solutions

  1. Set the Member property (e.g. "System.Math.PI") or use the StaticExtension(string) constructor before calling ProvideValue.
  2. Alternatively set MemberType plus the member name when using the MemberType-based resolution path.
  3. Guard: check extension.Member for null and fail fast in your own code before invoking ProvideValue.

Example fix

// before
var ext = new StaticExtension();
var v = ext.ProvideValue(provider); // throws
// after
var ext = new StaticExtension { Member = "System.Math.PI" };
var v = ext.ProvideValue(provider);
Defensive patterns

Strategy: type-guard

Validate before calling

if (ext.Member is null && ext.MemberType is null)
    throw new InvalidOperationException("StaticExtension requires Member (or MemberType + member) before ProvideValue");

Type guard

bool CanProvideStaticValue(StaticExtension ext) => ext is not null && ext.Member is not null;

Try / catch

try { var v = ext.ProvideValue(provider); }
catch (InvalidOperationException) { /* Member unset — initialize and retry or fail with context */ }

Prevention

When it happens

Trigger: Calling ProvideValue on a StaticExtension created with the parameterless constructor where Member was never set (e.g. new StaticExtension().ProvideValue(provider)).

Common situations: Programmatic XAML construction skipping the Member assignment; markup extension evaluation frameworks that instantiate extensions via the default constructor and expect deferred property population that never happens.

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/79d1dd20228ecbea. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/StaticExtension.cs:51

        /// The Prefix is optional, and refers to the XML prefix in a Xaml file.
        /// </summary>
        public StaticExtension(string member)
        {
            _member = member ?? throw new ArgumentNullException(nameof(member));
        }

        /// <summary>
        /// Return an object that should be set on the targetObject's targetProperty
        /// for this markup extension. For a StaticExtension this is a static field
        /// or property value.
        /// </summary>
        /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
        /// <returns> The object to set on this property.</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (_member is null)
            {
                throw new InvalidOperationException(SR.MarkupExtensionStaticMember);
            }

            Type type = MemberType;
            string fieldString;
            string typeNameForError = null;
            if (type is not null)
            {
                fieldString = _member;
                typeNameForError = type.FullName;
            }
            else
            {
                // Validate the _member
                int dotIndex = _member.IndexOf('.');
                if (dotIndex < 0)
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, _member));
                }

View on GitHub (pinned to 81131a70a4)