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

时间:2015-08-05 23:07:50

标签: c# .net

我试图做一些新事并得到这个错误,我在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;
    }
}

我收到以下错误:

  

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

有人可以帮我找到我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

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

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