Generics and Inheritance
See original GitHub issueConsider the following code in C#, it executes just fine. As the middle tier class instantiates the Storage class as an Array of TValue.
public class Storage<TValue> {
public TValue Values { get; set; }
}
public class BaseClass<TValue> : Storage<TValue[]> { }
public class FinalClass<TValue> {
private BaseClass<string> baseClass;
public FinalClass() => this.baseClass = new BaseClass<string> { Values = new string[] { } }; //Important Line of Code
}
Because the middle tier class defines the TValue parameter as string, it allows to set the Values property in the FinalClass as new string[]. However, this behavior breaks in Blazor, because of the RuntimeHelpers.TypeCheck method.
Storage.razor
@typeparam TValue
@code {
[Parameter]
public TValue Values { get; set; }
}
BaseClass.razor
@typeparam TValue
@inherits Storage<TValue[]>
FinalClass.razor
<BaseClass TValue="string" Values="@(new string[]{})"></BaseClass>
The error you receive is “cannot convert from ‘string[]’ to ‘TValue[]’”
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Generics, Inheritance, and Subtypes (The Java™ Tutorials ...
This beginner Java tutorial describes fundamentals of programming in the Java programming language.
Read more >Generics And Their Inheritance
When generic class extends another generic class, sub class should have at least same type and same number of type parameters and at...
Read more >Generic Class Hierarchies in Java
The subclass is a class that does inherit. It inherits all members defined by super-class and adds its own, unique elements. These uses...
Read more >Multilevel Java generic inheritance using extends on ...
When I contruct a Third , I want to be able to give the generic parameter as SomeConcreteClass (or derived class thereof), and...
Read more >How Do Generic Subtypes Work?
In this article, we will explore the various rules that surround subclassing generic types and build a cohesive view of the generic inheritance...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The repro for this can be simplified a lot, so it looks worse to me now. All you need is the following component:
You just can’t use this component. If you don’t specify the
T
parameter explicitly, the compiler complains that it can’t infer the type and you should specify it. But if you do specify it, like this:… then you get the same compiler error reported above:
Argument 1: cannot convert from 'string[]' to 'T[]'
. The generated code looks like this:… which is wrong because the compiler hasn’t substituted in the known value for
T
.I don’t know whether this would be a trivial fix or is just impossible given the design of the type checker, but it’s definitely incorrect, so reopening.
This should be resolved already by #34734