question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generics and Inheritance

See original GitHub issue

Consider 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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveSandersonMScommented, Jul 21, 2021

The repro for this can be simplified a lot, so it looks worse to me now. All you need is the following component:

    public class SomeComponent<T> : ComponentBase
    {
        [Parameter] public T[] Items { get; set; }
    }

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:

<SomeComponent T="string" Items="@(Array.Empty<string>())"></SomeComponent>

… then you get the same compiler error reported above: Argument 1: cannot convert from 'string[]' to 'T[]'. The generated code looks like this:

__builder.AddAttribute(
    1,
    "Items",
    global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<T[]>(
        Array.Empty<string>()));

… 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.

0reactions
mkArtakMSFTcommented, Sep 1, 2021

This should be resolved already by #34734

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found