简体   繁体   English

创建继承通用默认参数的Collection类

[英]Create Collection class which inherits generic default parameters

I would like to create a C# class outside a repository, which inherits all the default generic methods (Add, Remove All, etc). 我想在存储库外部创建一个C#类,该类继承了所有默认的通用方法(添加,全部删除等)。 This following code works. 以下代码有效。 My goal is to move List ShoppingCart outside the repository. 我的目标是将List ShoppingCart移至存储库之外。

public class CartLine
{
    public int CartLineId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
}

Following Code Works: 以下代码工作:

 public class ShoppingCartRepository
    {

        private List<CartLine> ShoppingCart = new List<CartLine>();


        public IEnumerable GetShoppingCart()
        {
            return ShoppingCart.ToList();
        }

        public virtual void AddItem(int productid, int quantity)
        {
            ShoppingCart.Add(new CartLine { ProductId = productid, Quantity = quantity });
        }

        public virtual void RemoveItem(int cartlineid)
        {
            ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
        }

This Code Does not work: "Error: Shopping Cart does not contain definition for ToList. " 该代码不起作用: “错误:购物车不包含ToList的定义。”

public class ShoppingCart : List<ShoppingCart>
{
    public ShoppingCart()
    {
        List<CartLine> ShoppingCart = new List<CartLine>();
    }
}


public class ShoppingCartRepository
{

    //private List<CartLine> ShoppingCart = new List<CartLine>();


    public IEnumerable GetShoppingCart()
    {
        return ShoppingCart.ToList();
    }

    public virtual void AddItem(int productid, int quantity)
    {
        ShoppingCart.Add(new CartLine { ProductId = productid, Quantity = quantity });
    }

    public virtual void RemoveItem(int cartlineid)
    {
        ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
    }

}

Maybe this would help you. 也许这会对您有所帮助。

public class CartLine
{
        public int CartLineId { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }
}

    public class ShoppingCart : List<CartLine>
    {
        public ShoppingCart()
        {
        }

    }

    public class ShoppingCartRepository
    {

        private ShoppingCart ShoppingCart = new ShoppingCart();

        public IEnumerable GetShoppingCart()
        {
            return ShoppingCart.ToList();
        }

        public virtual void AddItem(int productid, int quantity)
        {
            ShoppingCart.Add(new CartLine
            {
                ProductId = productid,
                Quantity = quantity
            });
        }

        public virtual void RemoveItem(int cartlineid)
        {
            ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
        }

    }

I change the class to make it generic. 我更改类以使其通用。 The Problem I think is, that you make your make a generic list of ShoppingCart but you want to add CartLine . 我认为的问题是,您要使ShoppingCart成为通用列表,但要添加CartLine

I hope this help you. 希望对您有所帮助。

I am not sure why are you trying to move Shopping cart outside, but there are many problems with the code 我不确定您为什么要将购物车移到外面,但是代码有很多问题

  1. You will need to define List<CartLine> ShoppingCart property in the class ShoppingCartRepository . 您将需要在List<CartLine> ShoppingCart class ShoppingCartRepository定义List<CartLine> ShoppingCart属性。 Right now you are creating it inside constructor. 现在,您正在内部构造函数中创建它。 That will not make ShoppingCart property/field of the class. 那不会使ShoppingCart成为类的属性/字段。
  2. As mentioned below in the comments, ToList() is not part of the List . 如以下注释中所述, ToList()不是List一部分。 So you will not be able to use it unless you explicitly define it. 因此,除非您明确定义它,否则将无法使用它。 But not sure why you need to call ToList() on a List element. 但不确定为什么需要在List元素上调用ToList()
  3. Also, you shouldn't inherit from List<ShoppingCart> , instead use interface - IList<T> . 另外,您不应该继承List<ShoppingCart> ,而应该使用interface- IList<T> That will help you with mocking/testing. 这将帮助您进行模拟/测试。

More importantly I don't see any value add in what you are trying to do. 更重要的是,我没有看到您尝试做的任何增值。 If you can provide more details around that, may be I can provide more details. 如果您可以提供更多详细信息,也许我可以提供更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在继承Collection &lt;&gt;的类中访问集合? - How to access the collection inside a class which inherits Collection<>? 从抽象类继承的类中的泛型列表上的Foreach循环 - Foreach loop on a generic list in a class which inherits from a abstract class 使用反射查找从通用类继承的类 - Find classes which inherits from a generic class using reflection 测试类是否继承通用接口 - Test if class inherits generic interface 具体类继承自抽象类,抽象类继承自通用抽象类 - Concrete Class inherit from Abstract class which inherits from Generic Abstract Class 如何将通用约束添加到从其他类继承的C#类? - How to Add Generic Constraint to C# class which inherits from other class? 如何为继承另一个 class 的通用 class 编写 nunit 测试用例(c#) - How to write nunit test case(c#) for generic class which inherits another class 在继承通用 class 的 c# 中生成 Class,该通用 class 使用 Telosys 代码生成工具使用类型参数 - Generating Class in c# that inherits a generic class which uses type parameter using Telosys code generation tool 类继承了通用字典 <string, IFoo> 和界面 - Class inherits generic dictionary<string, IFoo> and Interface 使用IEnumerable创建通用集合的通用类<T> - Generic Class to create a Generic Collection using IEnumerable<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM