C# 我可以添加一个列表吗<&燃气轮机;类的未知类型?

C# 我可以添加一个列表吗<&燃气轮机;类的未知类型?,c#,C#,我有一个类定义为: public class ReportObjectInformation { public string tableName { get; set; } public int progressBarValue { get; set; } public int rowCount { get; set; } public bool canConnect { get; set; } public int index { get; set; }

我有一个类定义为:

public class ReportObjectInformation
{
    public string tableName { get; set; }
    public int progressBarValue { get; set; }
    public int rowCount { get; set; }
    public bool canConnect { get; set; }
    public int index { get; set; }
    public string summaryFile { get; set; }
    public string reportFile { get; set; }
    public string errorFile { get; set; }
}
目前,我的代码中有七个不同的对象列表。有没有一种方法可以让我这样做:

public class ReportObjectInformation
{
    public string tableName { get; set; }
    public int progressBarValue { get; set; }
    public int rowCount { get; set; }
    public bool canConnect { get; set; }
    public int index { get; set; }
    public string summaryFile { get; set; }
    public string reportFile { get; set; }
    public string errorFile { get; set; }
    public List<> listOne { get; set; }  // add this
    public List<> listTwo { get; set; }  // add this
}
我在我的代码中创建为:

List<parameters> parm_list = new List<parameters>();

如果您可以保证
ReportObjectInformation
的特定实例将与给定类型的列表一起使用,则可以执行以下操作:

public class ReportObjectInformation<TOne, TTwo>
{
    public string tableName { get; set; }
    public int progressBarValue { get; set; }
    public int rowCount { get; set; }
    public bool canConnect { get; set; }
    public int index { get; set; }
    public string summaryFile { get; set; }
    public string reportFile { get; set; }
    public string errorFile { get; set; }
    public List<TOne> listOne { get; set; }  
    public List<TTwo> listTwo { get; set; }  
}
公共类ReportObjectInformation
{
公共字符串表名{get;set;}
public int progressBarValue{get;set;}
公共int行计数{get;set;}
公共bool canConnect{get;set;}
公共int索引{get;set;}
公共字符串摘要文件{get;set;}
公共字符串报告文件{get;set;}
公共字符串错误文件{get;set;}
公共列表列表一{get;set;}
公共列表listTwo{get;set;}
}

它允许您指定ReportObjectInformation对象列表要使用的类型。

您可以将ReportObjectInformation设置为通用

public class ReportObjectInformation<TOne, TTwo>
{
    public List<TOne> listOne { get; set; }  // add this
    public List<TTwo> listTwo { get; set; }  // add this
}
公共类ReportObjectInformation
{
公共列表列表一{get;set;}//添加此
公共列表listTwo{get;set;}//添加此
}
然后创建一个这样的实例

var reportInfo = new ReportObjectInformation<parameters, otherClass>();
reportInfo.listOne = new List<parameters>();
reportInfo.listTwo = new List<otherClass>();
var reportInfo=new reportobjectinfo();
reportInfo.listOne=新列表();
reportInfo.listwo=新列表();

当然,这意味着每个实例不能切换到保存其他类型的列表

好吧,因为您希望在运行时将不同类型的列表分配给对象,所以不需要进行类型检查。可以实现如下操作:

public class ContainingClass
{
    public IList SomeList { get; set; }
}
那你就可以了

var c = new ContainingClass();
c.SomeList = new List<int> { 1, 2, 3 };
c.SomeList = new List<string> { "abc", "def" };

foreach (var member in c.SomeList)
    Console.WriteLine(member);
var c=newcontainingclass();
c、 SomeList=新列表{1,2,3};
c、 SomeList=新列表{“abc”,“def”};
foreach(c.SomeList中的var成员)
控制台写入线(成员);
但您只能在万不得已的情况下这样做,通常更喜欢使用泛型或干净设计,因为这是:

  • 慢速-由于
    IList
    object
  • 不安全-谁知道在给定时间有什么列表?更不用说您将没有编译时类型检查(这些检查功能非常强大,请尽量不要丢失它们)

  • 一般认为这是不允许的,除非你真的真的没有选择(例如遗留代码兼容性)。

    我想你可能想制作<代码> ReaveTojCudioDebug < /Cord>通用,但是不清楚你在这里想要达到什么目的。什么是“7类列表”?7个列表只是7个不同类别的列表。把它想象成列表,列表,列表等等。。。但是我使用的不是那些基本的类(int、string、char),而是我创建的类。@JohnJenkins这7个类有什么关系吗?您希望列表仅包含一个
    ReportObjectInformation
    实例的一种类型的对象,还是所有7个类的混合?这7个类完全不相关。我想创建一个ReportObjectInformation并使用class1的列表。然后创建另一个ReportObjectInformation并使用class2的列表,依此类推。@juharr,好了,现在我有时间来思考这个问题了,我更好地理解了。我确实需要列表能够处理这七个类中的任何一个。这有意义吗?这正是我需要的!非常感谢。这正是我需要的!非常感谢。
    public class ContainingClass
    {
        public IList SomeList { get; set; }
    }
    
    var c = new ContainingClass();
    c.SomeList = new List<int> { 1, 2, 3 };
    c.SomeList = new List<string> { "abc", "def" };
    
    foreach (var member in c.SomeList)
        Console.WriteLine(member);