{"record":{"id":"05fd2842e0a97c10","repo":"dotnet/maui","slug":"declaringtype","errorCode":null,"errorMessage":"declaringType","messagePattern":"declaringType","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/BindableProperty.cs","lineNumber":197,"sourceCode":"\t\t\t{ typeof(bool), new[] { typeof(string) } },\n\t\t};\n\n\t\t/// <summary>A sentinel object used to indicate that a BindableProperty value has not been set.</summary>\n\t\tpublic static readonly object UnsetValue = new object();\n\n\t\tprivate static int _nextInternalId = int.MinValue;\n\t\tinternal readonly int InternalId;\n\n\t\tBindableProperty(string propertyName, [DynamicallyAccessedMembers(ReturnTypeMembers)] Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,\n\t\t\t\t\t\t\t\t ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,\n\t\t\t\t\t\t\t\t CoerceValueDelegate coerceValue = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null)\n\t\t{\n\t\t\tif (propertyName == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(propertyName));\n\t\t\tif (returnType is null)\n\t\t\t\tthrow new ArgumentNullException(nameof(returnType));\n\t\t\tif (declaringType is null)\n\t\t\t\tthrow new ArgumentNullException(nameof(declaringType));\n\t\t\t\n\t\t\tInternalId = Interlocked.Increment(ref _nextInternalId);\n\n\t\t\t// don't use Enum.IsDefined as its redonkulously expensive for what it does\n\t\t\tif (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime)\n\t\t\t\tthrow new ArgumentException($\"Not a valid type of BindingMode. Property: {returnType} {declaringType.Name}.{propertyName}. Default binding mode: {defaultBindingMode}\", nameof(defaultBindingMode));\n\n\t\t\tif (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.IsValueType)\n\t\t\t\tdefaultValue = Activator.CreateInstance(returnType);\n\n\t\t\tif (defaultValue != null && !returnType.IsInstanceOfType(defaultValue))\n\t\t\t\tthrow new ArgumentException($\"Default value did not match return type. Property: {returnType} {declaringType.Name}.{propertyName} Default value type: {defaultValue.GetType().Name}, \", nameof(defaultValue));\n\n\t\t\tif (defaultBindingMode == BindingMode.Default)\n\t\t\t\tdefaultBindingMode = BindingMode.OneWay;\n\n\t\t\tPropertyName = propertyName;\n\t\t\tReturnType = returnType;","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/BindableProperty.cs#L179-L215","documentation":"Argument null guard in the private BindableProperty constructor: declaringType must not be null. declaringType identifies the owner type of the property (used for attached properties and lookup), so null makes registration impossible. Reached via BindableProperty.Create / CreateAttached.","triggerScenarios":"BindableProperty.Create(nameof(X), typeof(int), null); CreateAttached with a null owner type; passing a typeof() on a type that was removed from the assembly.","commonSituations":"Defining an attached property in a class whose typeof() reference broke during a rename; multi-targeting where the owner type lives in a platform-specific assembly not referenced on the active target.","solutions":["Pass the declaring type, typically typeof(MyControl), as the third argument.","For attached properties use BindableProperty.CreateAttached with a concrete owner type.","Ensure the owner type assembly is referenced on every target framework."],"exampleFix":"// before\npublic static readonly BindableProperty ColumnProperty =\n    BindableProperty.CreateAttached(nameof(GetColumn), typeof(int), null, 0);\n\n// after\npublic static readonly BindableProperty ColumnProperty =\n    BindableProperty.CreateAttached(nameof(GetColumn), typeof(int), typeof(MyGrid), 0);","handlingStrategy":"validation","validationCode":"if (declaringType is null) throw new ArgumentNullException(nameof(declaringType));\nvar bp = BindableProperty.CreateAttached(nameof(GetCol), typeof(int), declaringType, 0);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass typeof(OwnerClass) explicitly for the declaring type.","For attached properties, prefer CreateAttached with the concrete owner type."],"tags":["maui","bindableproperty","argument-validation","attached-property","nullable"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}