- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个包含所有接口(interface)定义的项目:RivWorks.Interfaces
我有一个项目,我在其中定义了具体的实现:RivWorks.DTO
我以前做过数百次,但出于某种原因,我现在收到此错误:
Cannot implicitly convert type 'System.Collections.Generic.List<RivWorks.DTO.Product>' to 'System.Collections.Generic.List<RivWorks.Interfaces.DataContracts.IProduct>'
接口(interface)定义(缩写):
namespace RivWorks.Interfaces.DataContracts
{
public interface IProduct
{
[XmlElement]
[DataMember(Name = "ID", Order = 0)]
Guid ProductID { get; set; }
[XmlElement]
[DataMember(Name = "altID", Order = 1)]
long alternateProductID { get; set; }
[XmlElement]
[DataMember(Name = "CompanyId", Order = 2)]
Guid CompanyId { get; set; }
...
}
}
具体类定义(缩写):
namespace RivWorks.DTO
{
[DataContract(Name = "Product", Namespace = "http://rivworks.com/DataContracts/2009/01/15")]
public class Product : IProduct
{
#region Constructors
public Product() { }
public Product(Guid ProductID)
{
Initialize(ProductID);
}
public Product(string SKU, Guid CompanyID)
{
using (RivEntities _dbRiv = new RivWorksStore(stores.RivConnString).NegotiationEntities())
{
model.Product rivProduct = _dbRiv.Product.Where(a => a.SKU == SKU && a.Company.CompanyId == CompanyID).FirstOrDefault();
if (rivProduct != null)
Initialize(rivProduct.ProductId);
}
}
#endregion
#region Private Methods
private void Initialize(Guid ProductID)
{
using (RivEntities _dbRiv = new RivWorksStore(stores.RivConnString).NegotiationEntities())
{
var localProduct = _dbRiv.Product.Include("Company").Where(a => a.ProductId == ProductID).FirstOrDefault();
if (localProduct != null)
{
var companyDetails = _dbRiv.vwCompanyDetails.Where(a => a.CompanyId == localProduct.Company.CompanyId).FirstOrDefault();
if (companyDetails != null)
{
if (localProduct.alternateProductID != null && localProduct.alternateProductID > 0)
{
using (FeedsEntities _dbFeed = new FeedStoreReadOnly(stores.FeedConnString).ReadOnlyEntities())
{
var feedProduct = _dbFeed.AutoWithImage.Where(a => a.ClientID == companyDetails.ClientID && a.AutoID == localProduct.alternateProductID).FirstOrDefault();
if (companyDetails.useZeroGspPath.Value || feedProduct.GuaranteedSalePrice > 0) // kab: 2010.04.07 - new rules...
PopulateProduct(feedProduct, localProduct, companyDetails);
}
}
else
{
if (companyDetails.useZeroGspPath.Value || localProduct.LowestPrice > 0) // kab: 2010.04.07 - new rules...
PopulateProduct(localProduct, companyDetails);
}
}
}
}
}
private void PopulateProduct(RivWorks.Model.Entities.Product product, RivWorks.Model.Entities.vwCompanyDetails RivCompany)
{
this.ProductID = product.ProductId;
if (product.alternateProductID != null)
this.alternateProductID = product.alternateProductID.Value;
this.BackgroundColor = product.BackgroundColor;
...
}
private void PopulateProduct(RivWorks.Model.Entities.AutoWithImage feedProduct, RivWorks.Model.Entities.Product rivProduct, RivWorks.Model.Entities.vwCompanyDetails RivCompany)
{
this.alternateProductID = feedProduct.AutoID;
this.BackgroundColor = Helpers.Product.GetCorrectValue(RivCompany.defaultBackgroundColor, rivProduct.BackgroundColor);
...
}
#endregion
#region IProduct Members
public Guid ProductID { get; set; }
public long alternateProductID { get; set; }
public Guid CompanyId { get; set; }
...
#endregion
}
}
在另一个类(class)我有:
using dto = RivWorks.DTO;
using contracts = RivWorks.Interfaces.DataContracts;
...
public static List<contracts.IProduct> Get(Guid companyID)
{
List<contracts.IProduct> myList = new List<dto.Product>();
...
知道为什么会发生这种情况吗? (而且我确信这是非常简单的事情!)
最佳答案
是的,这是 C# 中的协变限制。您不能将一种类型的列表转换为另一种类型的列表。
代替:
List<contracts.IProduct> myList = new List<dto.Product>();
你必须这样做
List<contracts.IProduct> myList = new List<contracts.IProduct>();
myList.Add(new dto.Product());
Eric Lippert 解释了他们以这种方式实现的原因: http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx ( archived )
(以及为什么它与处理项目数组不同)。
关于C# - 无法将类型 List<Product> 隐式转换为 List<IProduct>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2640738/
我刚开始使用typescript,希望你能帮帮我,我不知道如何解决这个错误 接口(interface) export interface IProduct { name: string; pr
我是 typescript 的新手,我遇到了这个错误 Type 'Observable' is not assignable to type 'IProduct[]'. Property 'inclu
升级到 Angular 6.0 和 Rxjs 到 6.0 后,我收到以下编译错误: 属性“do”在“Observable”类型上不存在。 代码如下: import { Observable, of }
我有一个包含所有接口(interface)定义的项目:RivWorks.Interfaces 我有一个项目,我在其中定义了具体的实现:RivWorks.DTO 我以前做过数百次,但出于某种原因,我现在
好吧,我迷路了。为什么第一个函数是错误的(lambda 表达式中的波浪形),而第二个是正确的(意味着它可以编译)? public static Expression> IsValidExpre
我是一名优秀的程序员,十分优秀!