简体   繁体   English

Web服务的泛型方法和接口在C#?

[英]webservice generics method and interface in c#?

In a Windows Phone application, i consume multiple webservices from different providers. 在Windows Phone应用程序中,我使用了来自不同提供商的多个Web服务。 In order to bind the datas in the xaml, i have a class to rule them all. 为了将数据绑定到xaml中,我有一个类来统治所有这些。 Let's say : 比方说:

public class Event
{
  public string Id { get;set;}
  public string Title { get;set;}
  public string Latitude { get;set;}
  public string Longitude { get;set;}
}

When a provider has one webservice, i use a method like : 当提供程序具有一项Web服务时,我将使用如下方法:

private List<Event> MigrateCompanyEventToEvent(DataServiceCollection<CompanyEvent> collection)
{
   var listEvents = new List<Event>();
   foreach(var item in collection)
   {
      var ev = new Event();
      ev.Id = item.itemId.ToString(); //typeof Guid();
      ev.Title = item.companyEventTitle;
      if(item.latitude != null) { ev.Latitude = item.latitude.ToString(); } //typeof double?
      //etc.
      listEvents.Add(ev);
   }
   return listEvents;
}

But... I have a provider with a lot of similar webservices. 但是...我有一个提供很多类似Web服务的提供商。 I don't want to write a method for each one so i looked at generics methods. 我不想为每个方法编写一个方法,所以我研究了泛型方法。 I think i don't understand something. 我想我听不懂。

I have a webservices named KidEvents, ParentalEvents, SingleEvents... all with the same definition. 我有一个名为KidEvents,ParentalEvents,SingleEvents ...的Web服务,都具有相同的定义。 So, I wrote an Interface : 所以,我写了一个Interface:

public interface IdataEvents<T>
{
     Guid entity_id {get;}
     string nameEvent {get;}
     double? latitude {get;}
     double? longitude {get;}
}

And to proceed I wrote : 然后我写了:

private List<Event> MigrateGenericOdataToEvent<T>(DataServiceCollection<T> collection) where T:IdataEvents<T>
{
    var listEvents = new List<Event>();
    foreach(T item in collection)
    {
       var ev = new Event();
       ev.Title = item.nameEvent;
       ev.Id = item.entity_id;
       //etc.
       listEvents.Add(ev);
    }
    return listEvents
}

My problem is, when i use : var SingleEvents = MigrateGenericOdataToEvent(collection); 我的问题是,当我使用时:var SingleEvents = MigrateGenericOdataToEvent(collection); //with collection typeof DataServiceCollection //具有DataServiceCollection的集合类型

i get an error : 我得到一个错误:

Le type 'm3.ServiceReferenceData.SingleEvents' ne peut pas être utilisé 
comme paramètre de type 'T' dans le type ou la méthode générique 
'm3.ViewModels.MainViewModel.MigrateGenericOdataToEvent<T>
(System.Data.Services.Client.DataServiceCollection<T)'. 
Il n'y a pas de conversion de référence implicite de 
'm3.ServiceReferenceData.SingleEvents' en 
'm3.ViewModels.IdataEvents<m3.ServiceReferenceData.SingleEvents>'.  

Sorry, it's in french... It means something like : 抱歉,它是法文...意思是:

The type 'm3.ServiceReferenceData.SingleEvents' can not be used as a paramater 
of type 'T' in the type or generic method 
'm3.ViewModels.MainViewModel.MigrateGenericOdataToEvent<T>
(System.Data.Services.Client.DataServiceCollection<T)'. 
There is no implicit conversion reference of 'm3.ServiceReferenceData.SingleEvents' 
in 'm3.ViewModels.IdataEvents<m3.ServiceReferenceData.SingleEvents>'

Is it possible to parse datas from SingleEvents, ParentalEvents, KidEvents (all same data model) to one generic Event ? 是否可以将数据从SingleEvents,ParentalEvents,KidEvents(所有相同的数据模型)解析为一个通用事件? And how can i do that please ? 我该怎么办呢?

Thank you very much 非常感谢你

The error you are getting is because of this part of your function definition: 您收到的错误是由于函数定义的这一部分:

where T:IGenericOdata<T>

(Based on the exception message, I'm assuming that your actual code uses IdataEvents<T> and not IGenericOdata<T> , otherwise the exception doesn't make much sense.) (基于异常消息,我假设您的实际代码使用IdataEvents<T>而不是IGenericOdata<T> ,否则该异常没有多大意义。)

It is complaining that your data types do not actually implement the interface that you have in your constraint. 有人抱怨您的数据类型实际上没有实现约束中的接口。 You've told the compiler that only things that implement the interface are allowed to be used as T , but you are sending in a type that doesn't implement the interface. 您已经告诉编译器,只允许将实现该接口的内容用作T ,但是您发送的是未实现该接口的类型。

If these types are coming from an auto-generated service reference, you can take advantage of the fact that the data types are implemented as partial classes by the reference code generator, and extend them to implement your new interface by simply making another partial class file. 如果这些类型来自自动生成的服务引用,则可以利用以下事实:引用代码生成器将数据类型实现为部分类,并通过简单地制作另一个部分类文件来扩展它们以实现新接口。 。 Alternatively, you could create a wrapper class around each of the service reference classes and implement your interface there. 另外,您可以围绕每个服务引用类创建一个包装器类,并在那里实现您的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM