接口通用 return 类型
interface generic return type
我有几个 class 具有类似方法签名的 es,我希望在接口中捕获它们:
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 方法签名的接口,returns 一个 ICollection,类型未定义(因为每个列表都不同)。这将确保每个 *list 对象都有一个 fetch 方法,而新对象不会有 'getList' 或其他类似的命名调用。在做了一些研究之后,我相信泛型可能是可行的方法,但我不确定如何。
我试过了
public interface IDataRequest
ICollection<T> Fetch<T>();
但是当我将其实现为
public ICollection<Client> Fetch<Client>()
我在 'return 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
在获取时()
最后,如果我将其更改为:
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'.
我对泛型的了解不多,并且已经达到了我对 cargo cult 尝试的极限。我想做的事可行吗?如果是这样,你能告诉我接口方法签名和 class 方法定义的示例吗?
这里评论中要求的是客户端class:
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 ;
}
}
编译得很好。
我有几个 class 具有类似方法签名的 es,我希望在接口中捕获它们:
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 方法签名的接口,returns 一个 ICollection,类型未定义(因为每个列表都不同)。这将确保每个 *list 对象都有一个 fetch 方法,而新对象不会有 'getList' 或其他类似的命名调用。在做了一些研究之后,我相信泛型可能是可行的方法,但我不确定如何。
我试过了
public interface IDataRequest
ICollection<T> Fetch<T>();
但是当我将其实现为
public ICollection<Client> Fetch<Client>()
我在 'return 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
在获取时
最后,如果我将其更改为:
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'.
我对泛型的了解不多,并且已经达到了我对 cargo cult 尝试的极限。我想做的事可行吗?如果是这样,你能告诉我接口方法签名和 class 方法定义的示例吗?
这里评论中要求的是客户端class:
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 ;
}
}
编译得很好。