Skip to content

Generic type constraints always gives The type or namespace name ... could not be found #7148

@304NotModified

Description

@304NotModified

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying dotnet/aspnetcore#31800

This will always get a build error when using @typeparam T where T:IProductModel

CS0246 The type or namespace name 'IProductModel' could not be found (are you missing a using directive or an assembly reference?)

There are no intellisense errors

I tried:

  • using the full namespace
  • adding the namespace with @using
  • adding the namespace to _Imports.razor

Expected Behavior

No build error

Steps To Reproduce

I have this interface (in the same project as my Blazor Server)

public interface IProductModel
{
    int Code { get; }

    string Description { get; }
}

I have this component (stripped version)

@typeparam T where T:IProductModel

<select class="form-select">
	@foreach (var item in Items)
	{
		<option value="@item.Code" selected="@(item.Code == Selected.Code)">@item.Description</option>
	}
</select>
@code {
	[Parameter]
	public T Selected { get; set; } = default!;

	[Parameter]
	public IEnumerable<T> Items { get; set; } = default!;
}

Build the code.

See #7148

Exceptions (if any)

Only build errors

.NET Version

6.0.302

Anything else?

  • Visual Studio 2022 v17.2.6
  • Blazor Server .NET 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions