aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Support type constraints on @typeparam

Open neerajbharti opened this issue 8 years ago • 9 comments

This is in regards to the new @typeParam released in 0.6.0 version. I am not sure if Type Constraints can be defined on the generic type passed to @typeParam.

If this is something which already existing, can you please direct me to some semantics help. And if it is not there; is there any plan to support this.

This feature will be very much helpful while developing abstract components which must take "T" of some base type for example.

neerajbharti avatar Oct 04 '18 13:10 neerajbharti

This is something I'd like to do eventually, but it's not really straightforward to do right now.

~~If you need a workaround for this, you could create a base class in C# and then inherit it with @inherits~~

Actually, nevermind that won't work. You'd need to declare the type parameters on the derived class as well.

rynowak avatar Oct 04 '18 16:10 rynowak

Yeh.. I have already tried inheriting from base class, it didn't work for the very reason you have mentioned.

Right now I am progressing with keeping "T" to be of "anytype".

BTW, Templated Components feature is awesome. Loving it. Thank you for all your efforts.

neerajbharti avatar Oct 05 '18 04:10 neerajbharti

I don't know if what's been said already includes this but I'm currently sitting infront of the problem that after I had to mark my generic parameters as classes using where T : class, I would now have to ensure the same for @typeparam

SamVogelskamp avatar Nov 26 '18 13:11 SamVogelskamp

Same here, also searching for a way to add generic type constraints via @typeparam

DNF-SaS avatar Nov 29 '18 15:11 DNF-SaS

@rynowak Will this issue be mitigated by #5487 ? I.e. we can just put the generic constraints in the partial class?

srowan avatar May 13 '19 23:05 srowan

Just hit this myself, reduces the value proposition of templated components significantly with this limitation at present.

dazinator avatar Sep 06 '19 15:09 dazinator

Another vote. I agree value is limited without this...I’d like to allow templated components to render markup that’s common to a family of ViewModels by using properties defined in their base type, but without generic type constraints am unable to do this cleanly. This shortcoming wouldn't stand out as much if the rest of Blazor wasn't so damn sexy...appreciate what you guys are doing! =)

OkieCoder1 avatar Nov 27 '19 17:11 OkieCoder1

@srowan answer appears to be yes, but only after an explicit build (not at design time). The .razor.g.cs files don't have the constraint.

Given these two files:

@typeparam TItem

@code {
    [Parameter]
    public TItem Foo { get; set; }
}
public partial class TestComponent<TItem> where TItem : struct
{

}

The following will compile: <TestComponent Foo="1" TItem="int" />

But once you change the constraint to where TItem : class and do a full build, it won't.

CS0452 The type 'int' must be a reference type in order to use it as parameter 'TItem' in the generic type or method 'TestComponent'

So, @OkieCoder1, this may be a feasible workaround for now. You really only need a do-nothing partial class that contains the constraint, and to be aware that at design time, changes to the constraint won't properly be considered.

chucker avatar Nov 27 '19 22:11 chucker

We'll be handling this as part of https://github.com/dotnet/aspnetcore/issues/8433

mkArtakMSFT avatar Jan 10 '20 18:01 mkArtakMSFT