泛型:不能隐式转换类型

时间:2012-07-12 10:22:13

标签: c# generics

我有以下课程:

 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>()

下划线。为什么它明确地继承了界面?

更新:ERPListBase(已删除详细信息)

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

1 个答案:

答案 0 :(得分:1)

我已将您的代码粘贴到新项目中,并进行了一些小修改,例如ERPListBase - &gt;列表并编译。所以我的猜测是你有两个名为IWaybillDocument的接口,你在某处引用了错误的接口。