diff --git a/aspnetcore/blazor/components/index.md b/aspnetcore/blazor/components/index.md index df61df4117fb..79319ee4c515 100644 --- a/aspnetcore/blazor/components/index.md +++ b/aspnetcore/blazor/components/index.md @@ -1071,14 +1071,14 @@ For more information, see . > [!WARNING] > Providing initial values for component parameters is supported, but don't create a component that writes to its own parameters after the component is rendered for the first time. For more information, see . -Component parameters should be declared as *auto-properties*, meaning that they shouldn't contain custom logic in their `get` or `set` accessors. For example, the following `StartData` property is an auto-property: +Component parameters should be declared as [automatically-implemented properties (*auto properties*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties), meaning that they shouldn't contain custom logic in their `get` or `set` accessors. For example, the following `StartData` property is an auto property: ```csharp [Parameter] public DateTime StartData { get; set; } ``` -Don't place custom logic in the `get` or `set` accessor because component parameters are purely intended for use as a channel for a parent component to flow information to a child component. If a `set` accessor of a child component property contains logic that causes rerendering of the parent component, an infinite rendering loop results. +Don't place custom logic in the `get` or `set` accessor because component parameters are purely intended for use as a channel for a parent component to flow information to a child component. If a `set` accessor of a child component property contains logic that causes rerendering of the parent component, an infinite rendering loop results. Other side effects include unexpected extra renderings and parameter value overwrites. To transform a received parameter value: diff --git a/aspnetcore/diagnostics/bl0001.md b/aspnetcore/diagnostics/bl0001.md index 9575e1bfcb22..f9be1941fa5a 100644 --- a/aspnetcore/diagnostics/bl0001.md +++ b/aspnetcore/diagnostics/bl0001.md @@ -1,7 +1,7 @@ --- title: "BL0001: Component parameter should have public setters" description: "Learn about analysis rule BL0001: Component parameter should have public setters" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0002.md b/aspnetcore/diagnostics/bl0002.md index 031ab2853b11..86ca758dda24 100644 --- a/aspnetcore/diagnostics/bl0002.md +++ b/aspnetcore/diagnostics/bl0002.md @@ -1,7 +1,7 @@ --- title: "BL0002: Component has multiple CaptureUnmatchedValues parameters" description: "Learn about analysis rule BL0002: Component has multiple CaptureUnmatchedValues parameters" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0003.md b/aspnetcore/diagnostics/bl0003.md index 2f2019992ddc..d2f80d37ac0c 100644 --- a/aspnetcore/diagnostics/bl0003.md +++ b/aspnetcore/diagnostics/bl0003.md @@ -1,7 +1,7 @@ --- title: "BL0003: Component parameter with CaptureUnmatchedValues has the wrong type" description: "Learn about analysis rule BL0003: Component parameter with CaptureUnmatchedValues has the wrong type" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0004.md b/aspnetcore/diagnostics/bl0004.md index 6809b589133f..0796d47fd011 100644 --- a/aspnetcore/diagnostics/bl0004.md +++ b/aspnetcore/diagnostics/bl0004.md @@ -1,7 +1,7 @@ --- title: "BL0004: Component parameter should be public" description: "Learn about analysis rule BL0004: Component parameter should be public" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0005.md b/aspnetcore/diagnostics/bl0005.md index 408ca61d16c4..9d8bff7ae688 100644 --- a/aspnetcore/diagnostics/bl0005.md +++ b/aspnetcore/diagnostics/bl0005.md @@ -1,7 +1,7 @@ --- title: "BL0005: Component parameter should not be set outside of its component" description: "Learn about analysis rule BL0005: Component parameter should not be set outside of its component" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0006.md b/aspnetcore/diagnostics/bl0006.md index b7354ad31a97..c244f47237b8 100644 --- a/aspnetcore/diagnostics/bl0006.md +++ b/aspnetcore/diagnostics/bl0006.md @@ -1,7 +1,7 @@ --- title: "BL0006: Do not use RenderTree types" description: "Learn about analysis rule BL0006: Do not use RenderTree types" -author: pranavkm +author: guardrex monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.date: 10/21/2021 diff --git a/aspnetcore/diagnostics/bl0007.md b/aspnetcore/diagnostics/bl0007.md new file mode 100644 index 000000000000..5d1f22f80b83 --- /dev/null +++ b/aspnetcore/diagnostics/bl0007.md @@ -0,0 +1,34 @@ +--- +title: "BL0007: Component parameter '{0}' should be auto property" +description: "Learn about analysis rule BL0007: Component parameter '{0}' should be auto property" +author: guardrex +monikerRange: '>= aspnetcore-3.1' +ms.author: riande +ms.date: 04/07/2025 +uid: diagnostics/bl0007 +--- +# BL0007: Component parameter '{0}' should be auto property + +| | Value | +| - | - | +| **Rule ID** | BL0007 | +| **Category** | Usage | +| **Fix is breaking or non-breaking** | Non-breaking | + +## Cause + +A [component parameter](xref:blazor/components/index#component-parameters), which is a public [C# property](/dotnet/csharp/programming-guide/classes-and-structs/properties) of a component class with the [`[Parameter]` attribute](xref:Microsoft.AspNetCore.Components.ParameterAttribute), isn't an [automatically-implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). + +## Rule description + +A component parameter is a framework-managed communication channel between a parent and a child component. Developers shouldn't read or write to the parameter in a [`get` (getter) or `set` (setter) accessor](/dotnet/csharp/programming-guide/classes-and-structs/using-properties), either from inside or outside the component. + +Possible side effects of interacting directly with a component parameter include infinite rendering loops, unexpected extra renderings, and parameter value overwrites. + +## How to fix violations + +Implement the component parameter as an auto property and override or in the component class to read or transform the parameter's value. For more information, see the [`OnParametersSet{Async}` lifecycle method](xref:blazor/components/lifecycle#after-parameters-are-set-onparameterssetasync). + +## When to suppress warnings + +Do not suppress a warning from this rule. diff --git a/aspnetcore/diagnostics/code-analysis.md b/aspnetcore/diagnostics/code-analysis.md index 47389fb8df76..5e71a9e7dfea 100644 --- a/aspnetcore/diagnostics/code-analysis.md +++ b/aspnetcore/diagnostics/code-analysis.md @@ -49,6 +49,7 @@ Diagnostic ID: * [BL0004](xref:diagnostics/bl0004) * [BL0005](xref:diagnostics/bl0005) * [BL0006](xref:diagnostics/bl0006) +* [BL0007](xref:diagnostics/bl0007) * [MVC1000](xref:diagnostics/mvc1000) * [MVC1001](xref:diagnostics/mvc1001) * [MVC1002](xref:diagnostics/mvc1002) diff --git a/aspnetcore/toc.yml b/aspnetcore/toc.yml index df7e26b07dd9..043eb2aee566 100644 --- a/aspnetcore/toc.yml +++ b/aspnetcore/toc.yml @@ -1461,6 +1461,8 @@ items: uid: diagnostics/bl0005 - name: BL0006 uid: diagnostics/bl0006 + - name: BL0007 + uid: diagnostics/bl0007 - name: MVC1000 uid: diagnostics/mvc1000 - name: MVC1001