dotnet / dotnet/roslyn

Improve Blazor CascadingTypeParameter support - Current compiler cannot handle common scenarios.

Open
#85,552 4 comments 0 reactions 1 assignee Claimed by @jjonescz View on GitHub
Area-Razor Area-Razor-Compiler
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I expected the CascadingTypeParameter feature to work in the most common and obvious scenarios, but it does not. If the cascaded type was a string, it should be possible to use `````` instead of ``````. If you were passing in a parameter, it should be possible to do this `````` instead of needing to do this ``````. The CascadingTypeParameter feature was intended to resolve this type of issue but needs more work.

### Describe the solution you'd like

Imagine this repeater:
```razor
@attribute [CascadingTypeParameter(nameof(T))]
@typeparam T

@foreach (var item in Items)
{
@ChildContent(item)
}
@code {
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public IEnumerable Items { get; set;}
}
```
Then this Widget component intended to be used inside the repeater:
```razor
@typeparam T
Type is @typeof(T)
@code {
[Parameter] public EventCallback Action { get; set; }
[Parameter] public T? Item { get; set; }
}
```
Warning: Do not overthink this widget as it is not intended to do anything sensible, other than demonstrate compiler limitations.

Now imagine we use the component like such:
```razor

@code {
List items = ["David", "Fred", "Jimmy", "Banjo"];
}
```
The above code works, and `````` also works. However, it is completely reasonable to expect to be able to use the following syntax:
```razor

```

Another example of the compiler's mediocre compilation process is the following example. Let us say we write this code:
```razor

```
That works, but because T is a string, for consistency with the way the rest of blazor works, we would expect this to work (noting that it does work if the underlying type was a string instead of a type parameter, where the parameter was a string) :
```razor

```
However, this does not work (it looks for a member called Hello, which is what blazor would normally do for anything *other* than a string. Even this does NOT work: ``````.

@danroth27 and @SteveSandersonMS I had thought we were further along than this once we got to .NET 6 and added the CascadingTypeParameter feature. It is hard, not being on the compiler team, to understand why when an outer component uses CascadingTypeParameter, that that isn't the same as directly passing in the type using the old-school ```T="string"``` approach. But I do feel the current expectation means almost everyone will hit this issue and get stuck/frustrated.

Is there a concrete reason why the situation cannot be improved for .NET 9 or even .NET 10 if that is needed.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.