C# - 无法隐式转换类型List<产品与GT;列出< IProduct> [英] C# - Cannot implicitly convert type List<Product> to List<IProduct>

查看:115
本文介绍了C# - 无法隐式转换类型List<产品与GT;列出< IProduct>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我所有的接口定义一个项目:RivWorks.Interfaces结果
我有一个项目,我定义具体的implmentations:RivWorks.DTO

I have a project with all my Interface definitions: RivWorks.Interfaces
I have a project where I define concrete implmentations: RivWorks.DTO

我以前做过几百次,但由于某种原因,我现在收到此错误:

I've done this hundreds of times before but for some reason I am getting this error now:

无法隐式转换类型'System.Collections.Generic.List< RivWorks.DTO.Product>'以System.Collections.Generic.List< RivWorks.Interfaces.DataContracts.IProduct>

Cannot implicitly convert type 'System.Collections.Generic.List<RivWorks.DTO.Product>' to 'System.Collections.Generic.List<RivWorks.Interfaces.DataContracts.IProduct>'

接口定义(缩短):

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; }
        ...
    }
}

混凝土类定义(缩短):

Concrete class definition (shortened):

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
    }
}

在另一个类我有:

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

任何想法,为什么这可能发生? (而且我相信这是一些平凡简单!)

Any ideas why this might be happening? (And I am sure it is something trivially simple!)

推荐答案

是的它在C#中的协方差限制。不能在一个类型的列表转换为另一个列表。

Yep it's a covariance limitation in C#. You can't convert a list of one type to a list of another.

而不是:

List<contracts.IProduct> myList = new List<dto.Product>();

您必须这样做。

List<contracts.IProduct> myList = new List<contracts.IProduct>();

myList.Add(new dto.Product());

埃里克利珀解释了为什么他们实施这样说:
<一href=\"http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx\">http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

(以及为什么它是不同于与项目的数组时)。

(And why it is different than working with arrays of items).

这篇关于C# - 无法隐式转换类型List&LT;产品与GT;列出&LT; IProduct&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆