将任何类型的列表传递给另一个类

时间:2016-10-27 22:41:01

标签: c#

我正在构建一个动态搜索表单,我想将一个列表发送到搜索表单类(任何类型的列表)。我在这里搜索过,但没有找到任何答案。

头等舱:

public class myfirstclass
{
    Search search = new Search(List<anyType>);
    search.show()
}

搜索表单类:

public partial class Search : Form
{
    public search(list<anytype> mylist)
    {
    }
}

2 个答案:

答案 0 :(得分:1)

您可以更改构造函数参数以接受IList

List<Foo> somelist = new List<Foo>();
Search search = new Search(somelist);

public partial class Search : Form
{
    public Search(IList mylist)
    {
    }
}

答案 1 :(得分:0)

您可以使用generic class

public partial class Search<T> : Form
{
    public Search(List<T> mylist)
    {
    }
}

您还需要更新设计器类。

Search.designer.cs

partial class Search<T>
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        // code
    }

    #endregion
}