From f7561df085a12ffaad40f045e58408ed2b696c92 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Fri, 9 Jul 2021 07:22:58 -0500 Subject: [PATCH 1/2] Generic type support updates (including 6.0 updates) --- aspnetcore/blazor/components/index.md | 24 +++++++++++++++++ .../blazor/components/templated-components.md | 14 ++++++++-- aspnetcore/blazor/forms-validation.md | 6 +++++ .../webassembly-performance-best-practices.md | 6 +++++ aspnetcore/mvc/views/razor.md | 26 ++++++++++++++++++- 5 files changed, 73 insertions(+), 3 deletions(-) diff --git a/aspnetcore/blazor/components/index.md b/aspnetcore/blazor/components/index.md index 8de35bf1981c..3db9ce239bfd 100644 --- a/aspnetcore/blazor/components/index.md +++ b/aspnetcore/blazor/components/index.md @@ -1348,6 +1348,29 @@ Whitespace isn't preserved from the preceding markup: ::: moniker-end +## Generic type parameter support + +The [`@typeparam`][11] directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class: + +```razor +@typeparam TItem +``` + +::: moniker range=">= aspnetcore-6.0" + +C# syntax with [`where`](/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) type constraints is supported: + +```razor +@typeparam TEntity where TEntity : IEntity +``` + +For more information, see the following articles: + +* +* + +::: moniker-end + [1]: [2]: @@ -1359,3 +1382,4 @@ Whitespace isn't preserved from the preceding markup: [8]: [9]: [10]: +[11]: diff --git a/aspnetcore/blazor/components/templated-components.md b/aspnetcore/blazor/components/templated-components.md index 77be2605fde1..4f2697c7a28a 100644 --- a/aspnetcore/blazor/components/templated-components.md +++ b/aspnetcore/blazor/components/templated-components.md @@ -172,12 +172,22 @@ When receiving a cascaded type parameter, components obtain the parameter value Matching is only performed by name. Therefore, we recommend avoiding a cascaded generic type parameter with a generic name, for example `T` or `TItem`. If a developer opts into cascading a type parameter, they're implicitly promising that its name is unique enough not to clash with other cascaded type parameters from unrelated components. +Generic types with [`where`](/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) type constraints are supported: + +```razor +@typeparam TEntity where TEntity : IEntity +``` + +For more information, see the following articles: + +* +* + ::: moniker-end ::: moniker range="< aspnetcore-6.0" -> [!NOTE] -> Inferred generic types are supported in ASP.NET Core 6.0 or later. For more information, see a version of this article later than ASP.NET Core 5.0. +Inferred generic types are supported in ASP.NET Core 6.0 or later. For more information, see the 6.0 or a later version of this article. ::: moniker-end diff --git a/aspnetcore/blazor/forms-validation.md b/aspnetcore/blazor/forms-validation.md index d17a54c6c590..ee7ee0955a18 100644 --- a/aspnetcore/blazor/forms-validation.md +++ b/aspnetcore/blazor/forms-validation.md @@ -1055,6 +1055,12 @@ When working with radio buttons in a form, data binding is handled differently t } ``` +For more information on generic type parameters (`@typeparam`), see the following articles: + +* +* +* + The following `RadioButtonExample` component uses the preceding `InputRadio` component to obtain and validate a rating from the user: `Pages/RadioButtonExample.razor`: diff --git a/aspnetcore/blazor/webassembly-performance-best-practices.md b/aspnetcore/blazor/webassembly-performance-best-practices.md index 40509cbfff21..835563304355 100644 --- a/aspnetcore/blazor/webassembly-performance-best-practices.md +++ b/aspnetcore/blazor/webassembly-performance-best-practices.md @@ -238,6 +238,12 @@ To reduce this load, you could bundle together multiple parameters via custom cl In the preceding example, `Data` is different for every cell, but `Options` is common across all of them. Of course, it might be an improvement not to have a `` component and instead inline its logic into the parent component. +For more information on generic type parameters (`@typeparam`), see the following articles: + +* +* +* + #### Ensure cascading parameters are fixed The `` component has an optional parameter called `IsFixed`. diff --git a/aspnetcore/mvc/views/razor.md b/aspnetcore/mvc/views/razor.md index b92e06f365da..1f7a188d33e9 100644 --- a/aspnetcore/mvc/views/razor.md +++ b/aspnetcore/mvc/views/razor.md @@ -776,11 +776,35 @@ The `@key` directive attribute causes the components diffing algorithm to guaran Component references (`@ref`) provide a way to reference a component instance so that you can issue commands to that instance. For more information, see . +::: moniker-end + +::: moniker range=">= aspnetcore-6.0" + ### `@typeparam` *This scenario only applies to Razor components (.razor).* -The `@typeparam` directive declares a generic type parameter for the generated component class. For more information, see . +The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class. Generic types with [`where`](/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) type constraints are supported. + +For more information, see the following articles: + +* +* + +::: moniker-end + +::: moniker range="< aspnetcore-6.0 > aspnetcore-3.0" + +### `@typeparam` + +*This scenario only applies to Razor components (.razor).* + +The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class. + +For more information, see the following articles: + +* +* ::: moniker-end From d54d4d4b9487acba4cd2b71cfefc57c1dcaf1496 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Fri, 16 Jul 2021 04:26:37 -0500 Subject: [PATCH 2/2] Updates --- aspnetcore/blazor/components/index.md | 4 +- .../blazor/components/templated-components.md | 2 +- aspnetcore/mvc/views/razor.md | 44 ++++++++++++------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/aspnetcore/blazor/components/index.md b/aspnetcore/blazor/components/index.md index 3db9ce239bfd..f8752a87f5dc 100644 --- a/aspnetcore/blazor/components/index.md +++ b/aspnetcore/blazor/components/index.md @@ -1364,13 +1364,13 @@ C# syntax with [`where`](/dotnet/csharp/language-reference/keywords/where-generi @typeparam TEntity where TEntity : IEntity ``` +::: moniker-end + For more information, see the following articles: * * -::: moniker-end - [1]: [2]: diff --git a/aspnetcore/blazor/components/templated-components.md b/aspnetcore/blazor/components/templated-components.md index 4f2697c7a28a..e6348b3354b2 100644 --- a/aspnetcore/blazor/components/templated-components.md +++ b/aspnetcore/blazor/components/templated-components.md @@ -187,7 +187,7 @@ For more information, see the following articles: ::: moniker range="< aspnetcore-6.0" -Inferred generic types are supported in ASP.NET Core 6.0 or later. For more information, see the 6.0 or a later version of this article. +Inferred generic types are supported in ASP.NET Core 6.0 or later. For more information, see a 6.0 or later version of this article. ::: moniker-end diff --git a/aspnetcore/mvc/views/razor.md b/aspnetcore/mvc/views/razor.md index 1f7a188d33e9..91642883873e 100644 --- a/aspnetcore/mvc/views/razor.md +++ b/aspnetcore/mvc/views/razor.md @@ -452,7 +452,7 @@ The `@attribute` directive adds the given attribute to the class of the generate ### `@code` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* The `@code` block enables a [Razor component](xref:blazor/components/index) to add C# members (fields, properties, and methods) to a component: @@ -594,7 +594,7 @@ The `@inject` directive enables the Razor Page to inject a service from the [ser ### `@layout` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* The `@layout` directive specifies a layout for routable Razor components that have an [`@page`](#page) directive. Layout components are used to avoid code duplication and inconsistency. For more information, see . @@ -722,25 +722,25 @@ Razor directive attributes are represented by implicit expressions with reserved ### `@attributes` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* `@attributes` allows a component to render non-declared attributes. For more information, see . ### `@bind` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Data binding in components is accomplished with the `@bind` attribute. For more information, see . ### `@bind:culture` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Use the `@bind:culture` attribute with the [`@bind`](#bind) attribute to provide a for parsing and formatting a value. For more information, see . ### `@on{EVENT}` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Razor provides event handling features for components. For more information, see . @@ -750,13 +750,13 @@ Razor provides event handling features for components. For more information, see ### `@on{EVENT}:preventDefault` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Prevents the default action for the event. ### `@on{EVENT}:stopPropagation` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Stops event propagation for the event. @@ -766,13 +766,13 @@ Stops event propagation for the event. ### `@key` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* The `@key` directive attribute causes the components diffing algorithm to guarantee preservation of elements or components based on the key's value. For more information, see . ### `@ref` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* Component references (`@ref`) provide a way to reference a component instance so that you can issue commands to that instance. For more information, see . @@ -782,9 +782,19 @@ Component references (`@ref`) provide a way to reference a component instance so ### `@typeparam` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* + +The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class: + +```razor +@typeparam TEntity +``` -The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class. Generic types with [`where`](/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) type constraints are supported. +Generic types with [`where`](/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) type constraints are supported: + +```razor +@typeparam TEntity where TEntity : IEntity +``` For more information, see the following articles: @@ -793,13 +803,17 @@ For more information, see the following articles: ::: moniker-end -::: moniker range="< aspnetcore-6.0 > aspnetcore-3.0" +::: moniker range="< aspnetcore-6.0 >= aspnetcore-3.0" ### `@typeparam` -*This scenario only applies to Razor components (.razor).* +*This scenario only applies to Razor components (`.razor`).* + +The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class: -The `@typeparam` directive declares a [generic type parameter](/dotnet/csharp/programming-guide/generics/generic-type-parameters) for the generated component class. +```razor +@typeparam TEntity +``` For more information, see the following articles: