Why type parameter order is important when using components?

132 views Asked by At

VS 2022 (17.6.2), .NET 6,7.

It seems like you can't assign them in a random order when use a componen like this:

<MyComponent 
    T1="MyClass1"
    T2="MyClass2"
    T3="MyClass3" />

For example:

component's code behind:

public partial class MyComponent<T1, T2, T3>
    where T1 : Class1
    where T2 : Class2<T1>, IMyInterface
    where T3 : Class3, new()

component's markup:

@inherits MyComponent<T1, T2, T3>
@typeparam T1 where T1 : Class1
@typeparam T2 where T2 : Class2<T1>, IMyInterface
@typeparam T3 where T3 : Class3, new()

You can use this component as it showed in the first example only, not with a random type parameters order, like this or this:

<MyComponent 
    T3="MyClass3"
    T2="MyClass2"
    T1="MyClass1" />

<MyComponent 
    T2="MyClass2"
    T1="MyClass1"
    T3="MyClass3" />

For the first case in the code above you get an error

Error CS0311 The type 'MyClass3' cannot be used as type parameter 'T1' in the generic type or method 'MyComponent<T1, T2, T3>'. There is no implicit reference conversion from 'MyClass3' to 'Class1'.

This shows that the order is important. But why? Some bug in the VS Razor markup parser?

The project compiles and runs, but errors stay still until you close the markup file with using of your component. Then they disappear from the error list window.

0

There are 0 answers