我有以下类别:泛型:类型不能隐式转换
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