我可以添加一个List<>一类不知名的类型?

时间:2015-03-12 19:55:36

标签: 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 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
}

然后在我的代码中将列表设置为我的七种预定义类型的列表之一?

我的其他一个列表由这个类组成:

class parameters
{
    public string firstName{ get; set; }
    public string lastName{ get; set; }
    public string address{ get; set; }
    public string city{ get; set; }
    public string state { get; set; }
    public string country{ get; set; }
    public string active_flag { get; set; }
}
我在我的代码中创建的

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

parm_list填充了数据。现在我想将该列表添加到我正在创建的其他对象中。在我的代码的其他时间,我想将列表设置为我的其他类型之一,但现在我该怎么做?这甚至可能吗?

ReportObjectInformation reportObject = new ReportObjectInformation();
reportObject.tableName = "UserInformation";
reportObject.listOne = parm_list;
reportObject.listTwo = someother_list;

3 个答案:

答案 0 :(得分:1)

如果您可以保证ReportObjectInformation的特定实例可以使用给定类型的List,则可以执行此操作:

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对象列表要使用的类型。

答案 1 :(得分:1)

您可以ReportObjectInformation通用

public class ReportObjectInformation<TOne, TTwo>
{
    public List<TOne> listOne { get; set; }  // add this
    public List<TTwo> listTwo { get; set; }  // add this
}

然后创建一个像这样的实例

var reportInfo = new ReportObjectInformation<parameters, otherClass>();
reportInfo.listOne = new List<parameters>();
reportInfo.listTwo = new List<otherClass>();

当然这意味着每个实例都无法切换到保存其他类型之一的列表。

答案 2 :(得分:0)

好吧,既然您想在运行时为对象分配不同类型的列表,那么您将不会进行类型检查。可以实现如下:

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);

但是你应该只作为最后的手段,通常更喜欢使用泛型或干净的设计,因为这是:

  1. 慢 - 自IList使用object
  2. 以来,您必须投入很多
  3. 不安全 - 谁知道在给定时间有什么列表?更不用说你没有编译时类型检查(那些非常强大,尽量不要丢失它们)。
  4. 通常认为这是禁止的,除非你真的别无选择(例如遗留代码兼容性)。