C# C类的单个对象与对象列表

C# C类的单个对象与对象列表,c#,C#,我有一个从接口继承的类,实现了一个保存单个对象的方法和另一个保存类型为“iX”的对象列表的方法:- public class XX : iX { public override string A; public override string B; internal Some_Method(iX item) { A = item.A; B - item.B; } private void Single_Save(specific_type x

我有一个从接口继承的类,实现了一个保存单个对象的方法和另一个保存类型为“iX”的对象列表的方法:-

public class XX : iX
{

  public override string A;
  public override string B;

  internal Some_Method(iX item)
    {
       A = item.A; B - item.B;
    }

   private void Single_Save(specific_type x)
    {
      //// some code here ///
    }

   private void Multiple_Save(List<specific_type> x)
    {
      /// some code here ///
    } 

   internal void Save()
    {
      var y = AnotherFunction(this);
      Save(y);
    }

   internal void SaveAll()
    {
      Multiple_Save(that_list);   <----------- how do I get that list ??
    }
}
单一保存有效。但我在编写代码时遇到了一些问题,这些代码允许我保存对象列表:-

public static void SaveObjects(List<iX> items)
 {
    var multipleobj = new XX(items);   <------- this will not work, because my class is expecting a single object of type iX....
    multipleobj.SaveAll();
 }
**需要注意的是,多个保存方法和单个保存方法都使用不同形式的保存信息,需要由各自的方法单独使用,并且每次只能保存一个项目列表


有人能给我指出我应该考虑的概念来解决这个问题吗

如果您有一个单独的集合类,它会简单得多。让一个XX代表一个项目,如果您想将它们作为一个单元使用,请将它们放在一个集合中。创建一个只从列表继承的集合类很容易,它的所有方法和枚举器都可以从列表中获得。例如:

class XXList : List<XX>
{
    public void SaveAll()
    {
        foreach (var xx in this) xx.Save();
    }
}

类中需要多个构造函数。如果它们有不同的参数,方法重载允许您使用相同的名称创建多个方法

例如:

public class XX : IX
{
    public IX SingleItem { get; set; }
    public List<IX> ListOfItems { get; set; }
    public XX(IX item)
    {
        SingleItem = item;
    }
    public XX(List<IX> items)
    {
        ListOfItems = items;
    }
}

我不知道你想做什么。我看不到XX类的构造,但创建了一个重载构造函数

public XX(List<yourClass> item)
{

}

这将拥有您的对象。

您可以像下面这样使用:

// Create the list
List<ClassName> List = new List<Ix>();

// Create instance of your class
Ix ix = new Ix();

// Add the new object to the list
classList.Add(ix);
您还可以使用类型

在你的

private void Multiple_Save(List<specific_type> x)
{
  If (x != null)
  {
    foreach (var ix in x) 
        SingleSave(ix);
  /// some code here ///
  }
} 

我相信这需要System.Collections.Generic作为名称空间。

如果您能提供一个包含iX和XX构造函数的应用程序,那就太棒了。简单的回答是,您需要向XX添加一个新的构造函数,该构造函数获取您想要的列表。或者将SaveAll改为具有列表的参数。而且,别忘了,您可以创建一个包含单个对象的列表。
public XX(List<yourClass> item)
{

}
// Create the list
List<ClassName> List = new List<Ix>();

// Create instance of your class
Ix ix = new Ix();

// Add the new object to the list
classList.Add(ix);
private void Multiple_Save(List<specific_type> x)
{
  If (x != null)
  {
    foreach (var ix in x) 
        SingleSave(ix);
  /// some code here ///
  }
}