我有以下類別:泛型:類型不能隱式轉換
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() {}
}
您尚未提供ERPListBase的代碼。您對InsertAllOnSubmit的調用假定ERPListBase具有IList接口。不確定,但如果我們看到ERPListBase的代碼,它會有所幫助。 –
2012-07-12 10:31:31
waybills.ToEntitySetFromInterface(collection namehere) –
MMK
2012-07-12 10:31:53
btw。你可以簡化你向EntitySet添加項目的實現:'es.AddRange(source.Cast ());' – Rytmis 2012-07-12 10:43:26