2012-07-12 181 views
0

我有以下类别:泛型:类型不能隐式转换

public interface IWaybillDocument 
    { 
     long DocumentId { get; set; } 
     long BillingDocumentId { get; set; } 

     string ShipToName { get; set; } 
     byte AddressType { get; set; } 
     string City { get; set; } 
     string Country { get; set; } 
     string PostCode { get; set; } 
     string StateRegion { get; set; } 
     string Street1 { get; set; } 
     string Street2 { get; set; } 
     string Suburb { get; set; } 

     void MergeAddressing(Address address); 
    } 
} 

    public class WaybillDocumentList : ERPListBase<IWaybillDocument> 
    { 
     public WaybillDocumentList() : base() { } 
    } 

public partial class WaybillDocument : IWaybillDocument, INonrepudiable 
    { 

     public void MergeAddressing(Address address) 
     { 
      address.Street1 = this.Street1; 
      address.Street2 = this.Street2; 
      address.Suburb = this.Suburb; 
      address.City = this.City; 
      address.ZipCode = this.PostCode; 
      address.StateRegion = this.StateRegion; 
      address.Country = this.Country; 
      address.AddressKind = (AddressKind)this.AddressType; 
     } 
    } 

他们都编译很好,但尝试应用这种扩展方法时:

public static EntitySet<T> ToEntitySetFromInterface<T, U>(this IList<U> source) 
      where T : class, U 
     { 
      var es = new EntitySet<T>(); 
      IEnumerator<U> ie = source.GetEnumerator(); 
      while (ie.MoveNext()) 
      { 
       es.Add((T)ie.Current); 
      } 
      return es; 
     } 

像这样:

public void InsertWaybills(WaybillDocumentList waybills) 
     { 
      try 
      { 
       ; 
       this.Context.WaybillDocuments.InsertAllOnSubmit(waybills.ToEntitySetFromInterface<WaybillDocument, IWaybillDocument>()); 
       this.Context.SubmitChanges(); 
      } 
      catch (Exception ex) 
      { 
       throw new DALException("void InsertWaybills(WaybillList waybills) failed : " + ex.Message, ex); 
      } 
     } 

我收到编译错误

类型“WaybillDocument”不能用作 一般类型的类型参数“T”或方法 “LinqExtensions.ToEntitySetFromInterface(System.Collections.Generic.IList)”。 有从 'WaybillDocument' 没有隐式引用转换到 'IWaybillDocument'

waybills.ToEntitySetFromInterface<WaybillDocument, IWaybillDocument>() 

下划线。为什么当它明确地继承接口?

UPDATE:ERPListBase(详细信息删除)

public class ERPListBase<T> : List<T> 
{ 
    public ERPListBase() 
     : base() {} 
} 
+0

您尚未提供ERPListBase的代码。您对InsertAllOnSubmit的调用假定ERPListBase具有IList 接口。不确定,但如果我们看到ERPListBase的代码,它会有所帮助。 – 2012-07-12 10:31:31

+0

waybills.ToEntitySetFromInterface (collection namehere) – MMK 2012-07-12 10:31:53

+1

btw。你可以简化你向EntitySet添加项目的实现:'es.AddRange(source.Cast ());' – Rytmis 2012-07-12 10:43:26

回答

1

我贴你的代码,以新项目,并与像ERPListBase小的修改 - >列表并将其编译。所以我的猜测是你有两个名为IWaybillDocument的接口,并且你在某处引用了错误的接口。

+0

好吧,我刚刚得到了RedGate反射器最新版本,它也有一个VS插件,并以某种方式缓存旧代码并将其注入到构建中。因此,即使是在IDE中的所有内容都被正确地引用了,如果你看看输出反汇编,它实际上会引用代码文件和其他文件,这些文件是天之前的。实际上,实际上有3个同类和redgate反射器决定在构建中使用哪一个。我只想要一个体面的类浏览器,而不是一个无形的“手”。浪费时间。 :( – rism 2012-07-12 11:24:47