-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow generic type constraints #8433
Comments
I also think this is a much needed feature. Any comment on this? |
Without this feature, developers will be creating a lot of boilerplate components to workaround C# 8.0 #nullable limitations. for example, take this overly-simplified .razor:
And the class it's inheriting from:
The |
If Razor Components would create partial classes (last time I checked this was planned for 3.1.0) then you could define these generics including constraints in your own partial c# class. |
Isn't this solved now with 3.1 preview 1??? partial classes are published |
Someone already tried this? |
I have just tried it and it doesn't work for generic components at all. It works for basic partial class. This works
Doesn't work
|
This is a very important feature in my opinion. I wish if it can be done by .Net Core 3.1 |
@stsrki Can you explain what doesn't work? If I add Razor file MyClass.razor Code behind MyClass.razor.cs
Works as I would expect. |
@SQL-MisterMagoo I was getting an error:
You're right. If I add the |
Thanks for the solution @SQL-MisterMagoo ! I didn't think about templates, as I just needed properties from the constraints for my control. |
I used @SQL-MisterMagoo suggestion, and i have: Test.razor.cs But i get this error on build: Also, notice the generated Test.razor.g.cs constructor I'm probably doing something wrong since i just landed into Blazor, but i can't find the difference between your solution and mine |
I can confirm what @miggat said, I'm experiencing the same issue. This must be a bug. |
@miggat you need to be on 3.1 for the partial classes to work |
@SQL-MisterMagoo I am on 3.1.0-preview4.19579.2 and I have that error |
@miggat @FrancescoLorenzetti84 Here is a working sample for you https://github.com/SQL-MisterMagoo/BlazorPartialConstraint |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
We've moved this to |
Unless something changed since Feb 25, no, it's not. |
@joshlang You may need to explicitly specify the type argument when you use your generic component. E.g. <Fancy TItem="ItemType" Item="someItem"></Fancy> |
Actually I found out I was able to achieve what was needed with partial classes. In that case, maybe no need for changes because a person should use partial classes anyways. |
I wouldn't agree with that since there are plenty examples of components that require a generic constraint but do not really need a code-behind. It's good that we can achieve what we need right now, so this isn't a pressing issue. The end goal however should be that we can do this without code-behind. I'm totally fine if that comes later with 6.0. |
Thanks for contacting us. |
This has been implemented in a4a3668 and will be available in 6.0 preview4. |
We should be able to add generic constraints to our typeparam. For example:
@typeparam TEntity where TEntity : IEntity
As of preview3 gives the error "Unexpected literal following the 'typeparam' directive".
The text was updated successfully, but these errors were encountered: