创建继承通用默认参数的集合 class
Create Collection class which inherits generic default parameters
我想在存储库外创建一个 C# class,它继承了所有默认的通用方法(添加、全部删除等)。以下代码有效。我的目标是将 List ShoppingCart 移出存储库。
public class CartLine
{
public int CartLineId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
}
以下代码有效:
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);
}
此代码无效: "Error: Shopping Cart does not contain definition for 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);
}
}
也许这会对你有所帮助。
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);
}
}
我更改了 class 以使其通用。我认为的问题是,你制作了一个 ShoppingCart
的通用列表,但你想添加 CartLine
.
希望对你有所帮助。
我不知道你为什么要把购物车移到外面,但是代码有很多问题
- 您需要在
class ShoppingCartRepository
中定义 List<CartLine> ShoppingCart
属性。现在你正在构造函数中创建它。那不会使 ShoppingCart
property/field 的 class.
- 正如下面评论中提到的,
ToList()
不是 List
的一部分。因此,除非您明确定义它,否则您将无法使用它。但不确定为什么需要在 List
元素上调用 ToList()
。
- 此外,您不应该从
List<ShoppingCart>
继承,而是使用接口 - IList<T>
。这将帮助您 mocking/testing.
更重要的是,我没有看到您正在尝试做的事情有任何附加值。如果您可以提供更多详细信息,也许我可以提供更多详细信息。
我想在存储库外创建一个 C# class,它继承了所有默认的通用方法(添加、全部删除等)。以下代码有效。我的目标是将 List ShoppingCart 移出存储库。
public class CartLine
{
public int CartLineId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
}
以下代码有效:
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);
}
此代码无效: "Error: Shopping Cart does not contain definition for 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);
}
}
也许这会对你有所帮助。
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);
}
}
我更改了 class 以使其通用。我认为的问题是,你制作了一个 ShoppingCart
的通用列表,但你想添加 CartLine
.
希望对你有所帮助。
我不知道你为什么要把购物车移到外面,但是代码有很多问题
- 您需要在
class ShoppingCartRepository
中定义List<CartLine> ShoppingCart
属性。现在你正在构造函数中创建它。那不会使ShoppingCart
property/field 的 class. - 正如下面评论中提到的,
ToList()
不是List
的一部分。因此,除非您明确定义它,否则您将无法使用它。但不确定为什么需要在List
元素上调用ToList()
。 - 此外,您不应该从
List<ShoppingCart>
继承,而是使用接口 -IList<T>
。这将帮助您 mocking/testing.
更重要的是,我没有看到您正在尝试做的事情有任何附加值。如果您可以提供更多详细信息,也许我可以提供更多详细信息。