stride3d/stride · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

GetDependencyProperties reflects over a DependencyObject's type to collect its DependencyProperties. It validates the source object is non-null and throws ArgumentNullException with parameter name 'source', since reflection cannot proceed without a type to inspect.

Solutions

  1. Null-check the DependencyObject before calling GetDependencyProperties.
  2. Ensure the element exists and is loaded (e.g. call after Loaded event) before reflecting.
  3. Fix whatever lookup returned null (name, resource key, DataContext binding).

Example fix

// before
var props = (control as DependencyObject).GetDependencyProperties();
// after
if (control is DependencyObject dep && dep != null)
    var props = dep.GetDependencyProperties();
Defensive patterns

Strategy: type-guard

Validate before calling

if (element == null) return Array.Empty<DependencyProperty>();

Type guard

DependencyObject EnsureVisual(DependencyObject o) => o ?? throw new ArgumentException("Element not found in visual tree");

Try / catch

try { props = element.GetDependencyProperties(); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { props = Array.Empty<DependencyProperty>(); }

Prevention

When it happens

Trigger: Calling the extension GetDependencyProperties on a null DependencyObject, e.g. a template lookup or FindName result that came back null.

Common situations: FindName/FindResource returning null at runtime; UI element not yet loaded when the extension is invoked; passing a DataContext that failed to resolve.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/64baf5aad9181852. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Extensions/DependencyObjectExtensions.cs:24

using System.Windows;
using System.Reflection;
using System.Windows.Media;
using Stride.Core.Annotations;

namespace Stride.Core.Presentation.Extensions
{
    public static class DependencyObjectExtensions
    {
        /// <summary>
        /// Retrieves the public static DependencyProperties.
        /// </summary>
        /// <param name="source">DependencyObject that contains the DependencyProperties to be retrieved.</param>
        /// <param name="includingParentProperties">Indicates whether the DependencyProperties declared in the parent classes have to be retrieved too.</param>
        /// <returns>Returns an array of DependencyProperty owned by the DependencyObject.</returns>
        [NotNull]
        public static DependencyProperty[] GetDependencyProperties([NotNull] this DependencyObject source, bool includingParentProperties = false)
        {
            if (source == null) throw new ArgumentNullException(nameof(source));

            // there is probably a better way using TypeDescriptor

            var dependencyPropertyType = typeof(DependencyProperty);

            var flags = BindingFlags.Public | BindingFlags.Static;
            if (includingParentProperties)
                flags |= BindingFlags.FlattenHierarchy;

            return source.DependencyObjectType.SystemType.GetFields(flags)
                .Where(fi => fi.MemberType == MemberTypes.Field && fi.FieldType == dependencyPropertyType)
                .Select(fi => (DependencyProperty)fi.GetValue(source))
                .OrderBy(dp => dp.Name)
                .ToArray();
        }

        /// <summary>
        /// Sets the value of a DependencyProperty on a DependencyObject and all its logical children.

View on GitHub (pinned to 96fad776d2)