C# 无法将MyModel类型隐式转换为System.Collections.Generic.List<;T>;
我有一个模型课,如下所示C# 无法将MyModel类型隐式转换为System.Collections.Generic.List<;T>;,c#,generics,C#,Generics,我有一个模型课,如下所示 public class MyModel { [SQLite.PrimaryKey] public int Id { get; set; } public string Name { get; set; } public string Status { get; set; } } public interface IService { List<MyModel> IGetEmployeeDetails(); } 一个
public class MyModel
{
[SQLite.PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
}
public interface IService
{
List<MyModel> IGetEmployeeDetails();
}
一个接口类,其中我有一个返回类型为MyModel类method的方法,如下所示
public class MyModel
{
[SQLite.PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
}
public interface IService
{
List<MyModel> IGetEmployeeDetails();
}
公共接口iSeries设备
{
列出IGetEmployeeDetails();
}
上述接口已在我的服务类中实现,如下所示:
public List<MyModel> IGetEmployeeDetails()
{
return _connection.Table<MyModel>().ToList();
}
公共列表IGetEmployeeDetails()
{
返回_connection.Table().ToList();
}
上面的实现对我来说一切都很好,但是当我尝试将接口方法更改为泛型返回类型时,我面临接口方法的返回类型问题,如下所示
从列表IGetEmployeeDetails()代码>到列出IGetEmployeeDetails()代码>我得到了
无法将类型MyModel
隐式转换为System.Collections.Generic.List
基本上,我想让我的接口方法返回类型为泛型,但我不确定如何在我的服务类中将结果从MyModel类型转换为泛型类型
仅供参考,我已经审理了以下案件
return\u connection.Table().ToList()代码>
return(List)\u connection.Table().ToList()代码>&等
任何帮助都要提前感谢
基本上我想让我的接口方法返回类型为泛型,但是
我不知道如何在我的服务类中从
MyModel
转换为泛型类型
您所寻找的是类级别的泛型,而不是您所尝试的函数级别的泛型
如果函数是泛型函数,则调用方指定一个T
,函数必须处理它。在您的情况下,您需要不同的iSeries设备
实现,每个实现都能够返回不同的集合。因此:
public interface IService<T>
{
List<T> GetEmployeeDetails();
}
公共接口iSeries设备
{
列出GetEmployeeDetails();
}
以及导出的:
public class DerivedService : IService<MyModel>
{
public List<MyModel> GetEmployeeDetails()
{
return _connection.Table<MyModel>().ToList();
}
}
公共类派生服务:IService
{
公共列表GetEmployeeDetails()
{
返回_connection.Table().ToList();
}
}
非常感谢@Gilad Green,这就像一个符咒:)