C# 泛型:无法隐式转换类型
我有以下课程:C# 泛型:无法隐式转换类型,c#,generics,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; }
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;
}
}
公共接口IWaybillDocument
{
长文档ID{get;set;}
长BillingDocumentId{get;set;}
字符串ShipToName{get;set;}
字节地址类型{get;set;}
字符串城市{get;set;}
字符串国家{get;set;}
字符串邮政编码{get;set;}
字符串StateRegion{get;set;}
字符串Street1{get;set;}
字符串Street2{get;set;}
字符串{get;set;}
无效合并地址(地址);
}
}
公共类WaybillDocumentList:ERPListBase
{
public WaybillDocumentList():base(){}
}
公共部分类WaybillDocument:IWaybillDocument,不可否认
{
公共地址(地址)
{
address.Street1=this.Street1;
address.Street2=this.Street2;
address.郊区=这个郊区;
address.City=this.City;
address.ZipCode=this.PostCode;
address.StateRegion=this.StateRegion;
address.Country=这个.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;
}
公共静态EntitySet到EntitySetFromInterface(此IList源代码)
其中T:class,U
{
var es=新的EntitySet();
IEnumerator ie=source.GetEnumerator();
while(即MoveNext())
{
添加((T)即当前);
}
返回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);
}
}
公共作废插入运单(运单文档列表运单)
{
尝试
{
;
this.Context.WaybillDocuments.InsertAllOnSubmit(waybills.ToEntitySetFromInterface());
this.Context.SubmitChanges();
}
捕获(例外情况除外)
{
抛出新的DaleException(“作废插入运单(运单列表运单)失败:”+ex.消息,ex);
}
}
我得到一个编译错误
类型“WaybillDocument”不能用作中的类型参数“T”
泛型类型或方法
'LinqExtensions.ToEntitySetFromInterface(System.Collections.Generic.IList)'。
没有从“WaybillDocument”到的隐式引用转换
“IWaybillDocument”
与
waybills.ToEntitySetFromInterface()
下划线。为什么它显然继承了接口
更新:ERPListBase(已删除详细信息)
公共类ERPListBase:列表
{
公共资源列表库()
:base(){}
}
我已将您的代码粘贴到新项目中,并进行了一些小的修改,如ERPListBase->List,它将编译。因此,我的猜测是,您有两个名为IWaybillDocument的接口,并且您在某处引用了其中一个错误的接口。您没有给出ERPListBase的代码。对InsertAllOnSubmit的调用假定ERPListBase具有接口IList。不是决定性的,但如果我们看到ERPListBase的代码,它会有所帮助。waybills.ToEntitySetFromInterface(此处的集合名称)顺便说一句。您可以简化将项目添加到EntitySet的实现:es.AddRange(source.Cast())代码>@Rob从类型的角度看不太重要,只是列表的一个简单子类。好吧,我刚刚得到了RedGate reflectors的最新版本,它也有一个VS插件,它以某种方式缓存旧代码并将其注入到构建中。因此,即使IDE中的所有内容都被正确引用,但如果您查看输出反汇编,它实际上引用了代码文件和杂项文件,已经有几天了。所以实际上有3个是同一级别的,红门反射器决定在构建时使用哪一个。我只是想要一个体面的班级浏览器,而不是一只看不见的“手”。总浪费时间:(
waybills.ToEntitySetFromInterface<WaybillDocument, IWaybillDocument>()
public class ERPListBase<T> : List<T>
{
public ERPListBase()
: base() {}
}