title: Using Generic Types with Inheritance in Blazor Component
body: <p>I'm trying to make a "generic list" component in Blazor, and want the component to be able to accept any object that derives from a base class. My code currently is as below;</p>
  
  <p><strong>Base Class:</strong></p>
  
  <pre><code>    public class Model
      {
  
          // PK for the record
          [Key]
          public int Id { get; set; }
  
          // holds the front-end name for the record
          [Required(ErrorMessage = "Name is required")]
          public string Name { get; set; } = "";
  
          // holds the date and time the record was created
          [Required(ErrorMessage = "Created Timestamp is required")]
          public DateTime Created { get; set; } = DateTime.Now;
  
          // holds the username of the person who created the record
          [Required(ErrorMessage = "Creating user is required")]
          public string CreatedBy { get; set; } = "";
  
  
          // holds the date and time the record was updated
          public DateTime Updated { get; set; } = new DateTime(0);
  
          // holds the username of the person who last updated the record
          public string UpdatedBy { get; set; } = "";
  
      }
  </code></pre>
  
  <p><strong>Derived Class:</strong></p>
  
  <pre><code>public class ModelDesc: Model
      {
  
          // holds the description of the stakeholder
          public string Description { get; set; }
  
      }
  </code></pre>
  
  <p>The component is defined as below;
  <strong>DisplayGenericList.razor:</strong></p>
  
  <pre><code>@typeparam T 
  
  
  &lt;h3&gt;DisplayGenericList&lt;/h3&gt;
  
  @foreach (T lpObj in ListItems)
  {
      &lt;span&gt;@lpObj.Name, @lpObj.Id&lt;/span&gt;
  }
  
  @code {
  
  
      [Parameter]
      /// &lt;summary&gt;
      /// Contains the list of items to be shown in the list
      /// &lt;/summary&gt;
      public List&lt;T&gt; ListItems { get; set; }
  }
  
  }
  </code></pre>
  
  <p>with <strong>DisplayGenericList.razor.cs</strong> as below;</p>
  
  <pre><code>    public partial class DisplayGenericList&lt;T&gt; where T:Model
      {
      }
  </code></pre>
  
  <p>This alone compiles OK, however, when I try and use the component on a page I get the following error;
  CS0314
  <code>The type 'T' cannot be used as type parameter 'T' in the generic type or method 'DisplayGenericList&lt;T&gt;'. There is no boxing conversion or type parameter conversion from 'T' to 'ProjectName.Data.Interfaces.Model'.</code></p>
  
  <p>The <strong>index.razor</strong> is just this;</p>
  
  <pre><code>@page "/"
  
  @using ProjectName.Data.Models
  
  @using ProjectName.Shared.Components.Generics
  
  &lt;h1&gt;Hello, world!&lt;/h1&gt;
  
  Welcome to your new app.
  
  
  
  &lt;hr /&gt;
  
  &lt;DisplayGenericList ListItems="lItems" /&gt;
  
  
  @code
  {
  
      private List&lt;ModelDesc&gt; lItems = new List&lt;ModelDesc&gt;()
      {
          new ModelDesc() {Name = "Item 1", Description = "Description 1", Created = DateTime.Now, CreatedBy = "User 1"},
          new ModelDesc() {Name = "Item 2", Description = "Description 2", Created = DateTime.Now, CreatedBy = "User 1"},
          new ModelDesc() {Name = "Item 3", Description = "Description 3", Created = DateTime.Now, CreatedBy = "User 2"},
          new ModelDesc() {Name = "Item 4", Description = "Description 4", Created = DateTime.Now, CreatedBy = "User 2"},
          new ModelDesc() {Name = "Item 5", Description = "Description 5", Created = DateTime.Now, CreatedBy = "User 3"}
      };
  
  }
  
  </code></pre>
  
  <p>I'm fairly new to Blazor, and so i suspect i'm doing something wrong, however can anyone advise on what I should be doing here to enforce (and use) the constraint on the component?</p>