首页 / C# / C#-接口通用返回类型
C#-接口通用返回类型
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-接口通用返回类型,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3410字,纯文字阅读大概需要5分钟。
内容图文
我有希望在接口中捕获的具有类似方法签名的服务器类:
namespace MyLib
public class ClientList
public ICollection<Client> Fetch() {
{
//do stuff
return this.CreateCollection();
}
private ICollection<Client> CreateCollection()
{
List<Client> clientList = new List<Client>();
// populate list
return clientList;
}
public class ProductList
public ICollection<Product> Fetch() {
//do stuff
return this.CreateCollection();
}
private ICollection<Product> CreateCollection()
{
List<Product> productList = new List<Product>();
// populate list
return productList ;
}
我想要一个带有用于Fetch的方法签名的接口,该接口返回ICollection,类型为undefined(因为每个列表都会有所不同).这将确保每个* list对象将具有fetch方法,而新对象将不会具有“ getList”或其他类似的命名调用.经过一些研究后,我相信仿制药可能是要走的路,但是我不确定如何.
我试过了
public interface IDataRequest
ICollection<T> Fetch<T>();
但是当我实现为
public ICollection<Client> Fetch<Client>()
我在“返回this.CreateCollection();”错误:
Cannot implicitly convert type
‘System.Collections.Generic.ICollection<MyLib.Client>’ to
‘System.Collections.Generic.ICollection<Client>’. An explicit
conversion exists (are you missing a cast?)
我以为是因为我没有指定名称空间.但是如果我改为
public ICollection<MyLib.Client> Fetch<MyLib.Client>()
然后我得到了错误:
Type parameter declaration must be an identifier not a type
在Fetch< MyLib.Client>()上
最后,如果我将其更改为:
public ICollection<MyLib.Client> Fetch<Client>()
然后我得到了错误:
‘MyLib.ClientList’ does not implement interface member ‘MyLib.IDataRequest.Fetch()’. ‘MyLib.ClientList.Fetch()’ cannot implement ‘MyLib.IDataRequest.Fetch()’ because it does not have the matching return type of ‘System.Collections.Generic.ICollection’.
我对泛型没有很深的了解,并且已经达到了我可以尝试的极力尝试.我想做的事可能吗?如果可以,可以同时给我展示接口方法签名和类方法定义的示例.
根据注释的要求,这里是客户端类:
namespace MyLib
{
using System.Data;
using System.Runtime.Serialization;
[DataContract]
public class Client
{
public Client(DataRow clientRecord)
{
this.clientId = clientRecord.Field<string>("ID");
this.cphh = clientRecord.Field<string>("ID");
this.name = clientRecord.Field<string>("Name");
this.address = clientRecord.Field<string>("Address");
if (CommonUtilities.GIS.ValidateOSMapRef(clientRecord.Field<string>("Locationd")))
{
this.location = string.Format(
"{0}, {1}",
CommonUtilities.GIS.ConvertMapRefToEasting(clientRecord.Field<string>("Locationd")),
CommonUtilities.GIS.ConvertMapRefToNorthing(clientRecord.Field<string>("Locationd")));
}
}
[DataMember]
public string clientId { get; private set; }
[DataMember]
public string name { get; private set; }
[DataMember]
public string address { get; private set; }
[DataMember]
public string location { get; private set; }
[DataMember]
public string cphh { get; private set; }
}
}
解决方法:
您定义的接口不正确.像这样尝试:
public interface IDataRequest<T>
{
ICollection<T> Fetch();
}
然后您的类如下所示:
public class ClientList : IDataRequest<Client>
{
public ICollection<Client> Fetch()
{
//do stuff
return this.CreateCollection();
}
private ICollection<Client> CreateCollection()
{
List<Client> clientList = new List<Client>();
// populate list
return clientList;
}
}
public class ProductList : IDataRequest<Product>
{
public ICollection<Product> Fetch()
{
//do stuff
return this.CreateCollection();
}
private ICollection<Product> CreateCollection()
{
List<Product> productList = new List<Product>();
// populate list
return productList ;
}
}
编译效果很好.
内容总结
以上是互联网集市为您收集整理的C#-接口通用返回类型全部内容,希望文章能够帮你解决C#-接口通用返回类型所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。