c# - 如何将通用类型数据列表转换为另一个通用类型数据列表

标签 c# .net entity-framework generics

我正在尝试编写一个通用基础服务类,在收到第一个通用数据列表后,作为Db 模型实体的实际类型需要转换为新的通用 View 模型 类型的数据。

我试过了list.ConvertAll()但总是得到 ConvertAll() 的构建错误方法。

我也试过list.Cast<TVm>().ToList()这解决了构建错误但出现运行时错误。

这是我所有类和接口(interface)的代码片段。感谢您提供任何帮助或建议。

实体类

public abstract class Entity
{
    [Key]
    [Index("IX_Id", 1, IsUnique = true)]
    public string Id { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime Created { get; set; }

    public string CreatedBy { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime Modified { get; set; }

    public string ModifiedBy { get; set; }

    [DefaultValue(true)]
    public bool Active { get; set; }
}

BaseViewModel 类

public abstract class BaseViewModel<T> where T: Entity
{        

    protected BaseViewModel() { }

    protected BaseViewModel(T model)
    {
        Id = model.Id;
        Created = model.Created;
        CreatedBy = model.CreatedBy;
        Modified = model.Modified;
        ModifiedBy = model.ModifiedBy;
        Active = model.Active;
    }

    public string Id { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime Modified { get; set; }
    public string ModifiedBy { get; set; }
    public bool Active { get; set; }
}

IBaseService 接口(interface)

public interface IBaseService<T, TVm> where T : Entity where TVm : BaseViewModel<T>
{
    List<TVm> GetAll();
}

基础服务类

public abstract class BaseService<TEntity, TVm> : IBaseService<TEntity, TVm> where TEntity: Entity where TVm : BaseViewModel<TEntity>
{
    protected  IBaseRepository<TEntity> Repository;

    protected BaseService(IBaseRepository<TEntity> repository)
    {
        Repository = repository;
    }

    public virtual  List<TVm> GetAll()
    {
        List<TVm> entities;
        try
        {
            List<TEntity> list = Repository.GetAll().ToList();
            entities =  list.Cast<TVm>().ToList(); //runtime error
            entities = list.ConvertAll(x => new TVm(x)); //build error
            entities = list.ConvertAll(new Converter<TEntity, TVm>(TEntity)); //build error
        }
        catch (Exception exception)
        {                
            throw new Exception(exception.Message);
        }

        return entities;
    }

}

最佳答案

要创建通用类型的实例,您需要对其使用 new() 约束。但是,这不允许您向它传递任何参数。你也可以试试

  1. 像这样使用Activator创建实例

    entities = list.ConvertAll(x => (TVm)Activator.CreateInstance(typeof(TVm), x));

  1. BaseService 类签名中为 TVm 添加 new() 约束,并在传递给它的类上添加一个方法作为 TVm,它基本上执行当前构造函数执行的操作,但在一个方法中,并在创建新对象后调用它。

关于c# - 如何将通用类型数据列表转换为另一个通用类型数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521563/

相关文章:

c# - 如何在运行时使用 Entity Framework 在sql数据库中创建表并在运行时更新模型?

c# - 使用 autofixture 创建结构不会引发公共(public)构造函数错误

.net - 斯坦福 NLP 库是否有 native .NET 实现?

c# - 将单个对象转换为多个对象 c#

c# - Entity Framework 不遵守命令超时

c# - Entity Framework 的持续交付和数据库模式变化

c# - LINQ to SQL 插入顺序 GUID

c# - .Net4 中的 Httputility.urldecode 替换?

c# - Windows 窗体组合框 - 数据绑定(bind)到多个属性

c# - 使用 com interop 从 VBA 调用 C# dll 时出现类型不匹配错误