本文介绍了在Blazor组件中使用具有继承的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Blazor中创建一个泛型列表"组件,并希望该组件能够接受从基类派生的任何对象.我的代码当前如下:

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;

基本类别:

    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; } = "";

    }

派生类:

public class ModelDesc: Model
    {

        // holds the description of the stakeholder
        public string Description { get; set; }

    }

该组件定义如下; DisplayGenericList.razor:

@typeparam T 


<h3>DisplayGenericList</h3>

@foreach (T lpObj in ListItems)
{
    <span>@lpObj.Name, @lpObj.Id</span>
}

@code {


    [Parameter]
    /// <summary>
    /// Contains the list of items to be shown in the list
    /// </summary>
    public List<T> ListItems { get; set; }
}

}

具有 DisplayGenericList.razor.cs ,如下所示;

    public partial class DisplayGenericList<T> where T:Model
    {
    }

仅此一项就可以编译,但是,当我尝试使用页面上的组件时,出现以下错误;CS0314类型'T'不能用作通用类型或方法'DisplayGenericList< T>'中的类型参数'T'.没有从"T"到"ProjectName.Data.Interfaces.Model"的装箱转换或类型参数转换.

This alone compiles OK, however, when I try and use the component on a page I get the following error;CS0314The type 'T' cannot be used as type parameter 'T' in the generic type or method 'DisplayGenericList<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'ProjectName.Data.Interfaces.Model'.

index.razor 就是这样;

@page "/"

@using ProjectName.Data.Models

@using ProjectName.Shared.Components.Generics

<h1>Hello, world!</h1>

Welcome to your new app.



<hr />

<DisplayGenericList ListItems="lItems" />


@code
{

    private List<ModelDesc> lItems = new List<ModelDesc>()
    {
        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"}
    };

}

我对Blazor还是很陌生,所以我怀疑我做错了什么,但是谁能建议我在这里应该做些什么,以强制执行(使用)组件上的约束?

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?

推荐答案

一种解决方法是在组件用法中显式添加TypeParam.因此代码与原始问题中的示例相同,但是按如下所示更改了组件的index.razor实现;

A workaround is to add the TypeParam explicitly in the component usage; so the same code as in the example in the original question, but changing the index.razor implementation of the component as follows;

<DisplayGenericList ListItems="lItems" T="ModelDesc" />

正如上面@Vencovsky所发现的那样,这已被标记为功能请求,并且虽然基础Blazor类支持类型约束,但编译器无法执行约束,除非按照上面的定义明确定义类型.

This has been flagged as a feature request, as @Vencovsky above has found, and while type constraints are supported in the underlying Blazor classes, the compiler is unable to enforce the constraint without the type being explicitly defined as per above.

这篇关于在Blazor组件中使用具有继承的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:14