【问题标题】:Must be a non abstract type with public parameterless constructor c#必须是具有公共无参数构造函数 c# 的非抽象类型
【发布时间】:2015-08-05 23:07:49
【问题描述】:

我正在尝试做一些新的事情并得到了这个错误,我在 c# 中有一个接口

public interface IRepository<TSource>: IDisposable where TSource: class
{
    TSource GetById(long id);

    List<TSource> GetAll();

    bool InsertOrUpdate(TSource source);

    bool Delete(long id);

    VM.RetornoGenerico<TSource> Upload(long id);
}

问题是当我将TSource 传递给我的RetornoGenerico 类时

public class RetornoGenerico<T> where T : new()
{
    public T Result { get; set; }
    public bool Success { get; set; }
    public string ErrorMsg { get; set; }
    public string IdError { get; set; }

    public RetornoGenerico()
    {
        Result: new T();
        Success = false;

    }

    public static RetornoGenerico<T> CloneError<TU>(RetornoGenerico<TU> resposta) where TU : new()
    {
        var retorno = new RetornoGenerico<T>()
        {
            Success = resposta.Success,
            Result = new T(),
            ErrorMsg = resposta.ErrorMsg,
            IdError = resposta.IdError

        };
        return retorno;
    }
}

我收到以下错误:

必须是具有公共无参数构造函数的非抽象类型

谁能帮我找出我的代码有什么问题?

【问题讨论】:

  • 您作为 T 传递的类型是什么?它是具有默认构造函数的具体类吗?

标签: c# .net


【解决方案1】:

由于您的班级有where T : new(),并且您将TSource 传递给该班级,所以TSource 必须具有相同的约束。

public interface IRepository<TSource>: IDisposable where TSource: class, new()

【讨论】:

  • 非常感谢……它暂时解决了我的问题。
猜你喜欢
  • 2014-05-18
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多