C#接口和泛型列表 [英] C# Interfaces and Generic Lists

查看:110
本文介绍了C#接口和泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义为一个接口:

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; }
        ...
        [XmlElement]
        [DataMember(Name = "buttonPositionCSS", Order = 14)]
        string buttonPositionCSS { get; set; }
    }
}



我有一个具体的实现,如:

I have a concrete implementation like:

namespace RivWorks.Model.Objects
{
    [DataContract(Name = "Product", Namespace = "http://rivworks.com/DataContracts/2009/01/15")]
    public class Product : IProduct
    {
        #region Declarations
        private Guid _productID;
        private long _altProductID;
        private Guid _companyId;
        ...
        private string _buttonPositionCSS;
        #endregion

        #region IProduct Members
        public Guid ProductID { get { return _productID; } set { _productID = value; } }
        public long alternateProductID { get { return _altProductID; } set { _altProductID = value; } }
        public Guid CompanyId { get { return _companyId; } set { _companyId = value; } }
        ...
        public string buttonPositionCSS { get { return _buttonPositionCSS; } set { _buttonPositionCSS = value; } }
        #endregion
    }
}



我有另一个

I have another interface defined as:

namespace RivWorks.Interfaces.Services
{
    public interface IProductManager
    {
        #region Products
        IProduct GetProductById(Guid productId);
        List<IProduct> GetProductByCompany(Guid companyId);
        int SaveProduct(IProduct product);
        int DeleteProduct(Guid productId);
        #endregion
    }
}



我有一个类定义如:

I have a class defined as:

namespace RivWorks.Controller
{
    public class ProductManager : IProductManager
    {
        #region Declare Models
        private static RivWorks.Model.Negotiation.RIV_Entities _dbRiv = RivWorks.Model.Stores.RivEntities(AppSettings.RivWorkEntities_connString);
        private static RivWorks.Model.NegotiationAutos.RivFeedsEntities _dbFeed = RivWorks.Model.Stores.FeedEntities(AppSettings.FeedAutosEntities_connString);
        #endregion

        #region Products
        public IProduct GetProductById(Guid productId)
        {
            // deleted for simplicity sake
            return product;
        }
        public List<IProduct> GetProductByCompany(Guid companyId)
        {
            var company = (from a in _dbRiv.Company where a.CompanyId == companyId select a).First();
            var companyDetails = from a in _dbRiv.AutoNegotiationDetails where a.CompanyId == companyId select a;
            // ################################################## //
            List<IProduct> productList = new List<RivWorks.Model.Objects.Product>();
            // ################################################## //
            // deleted for simplicity sake
            return productList;
        }
        public int SaveProduct(IProduct product)
        {
            return 0;  // stub
        }
        public int DeleteProduct(Guid productId)
        {
            return 0;  // stub
        }
        #endregion
    }
}

我在编译的时候得到这个错误:

I am getting this error at compile time:

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

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

系统是一个非常服务(WCF,的WebORB等)导向的制度,我想揭露接口作为我的合同。我有型号和放大器;控制器在.NET和使用服务的意见(代理人)到第三方的消费者(真正的视图)我。

The system is a very Service (WCF, WebOrb, etc) oriented system and I wanted to expose Interfaces as my contracts. I have the Model & Controller in .NET and am using the Services as Views (proxies) to 3rd party consumers (the real View).

我在想什么或做错了吗?

What am I missing or doing wrong?

推荐答案

尝试改变:

List<IProduct> productList = new List<RivWorks.Model.Objects.Product>();

要:

List<IProduct> productList = new List<IProduct>();



或者(如果你需要用列表的元素作为其具体实现,而不是以前的接口工作返回列表):

Or (if you need to work with elements of the List as their concrete implementations rather than the Interface before you return the list):

List<RivWorks.Model.Objects.Product> productList = 
    new List<RivWorks.Model.Objects.Product>();

// Do some work here.

return productList.Cast<IProduct>().ToList();

这篇关于C#接口和泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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