C# 将任何类型的列表传递给另一个类
我正在构建一个动态搜索表单,我想向搜索表单类(任何类型的列表)发送一个列表。我在这里搜索了一下,没有找到任何答案 头等舱:C# 将任何类型的列表传递给另一个类,c#,C#,我正在构建一个动态搜索表单,我想向搜索表单类(任何类型的列表)发送一个列表。我在这里搜索了一下,没有找到任何答案 头等舱: 公共类myfirstclass { 搜索=新搜索(列表); search.show() } 搜索表单类: 公共部分类搜索:表单 { 公共搜索(列表mylist) { } } 您可以更改构造函数参数以接受IList: List<Foo> somelist = new List<Foo>(); Search search = new Search(so
公共类myfirstclass
{
搜索=新搜索(列表);
search.show()
}
搜索表单类:
公共部分类搜索:表单
{
公共搜索(列表mylist)
{
}
}
您可以更改构造函数参数以接受IList
:
List<Foo> somelist = new List<Foo>();
Search search = new Search(somelist);
public partial class Search : Form
{
public Search(IList mylist)
{
}
}
List somelist=newlist();
搜索=新搜索(somelist);
公共部分类搜索:表单
{
公共搜索(IList mylist)
{
}
}
您可以使用
公共部分类搜索:表单
{
公共搜索(列表mylist)
{
}
}
您还需要更新designer类
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
}
部分类搜索
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
//代码
}
#端区
}
尝试使用列表。它会告诉我此错误:当前列表中不存在名称“InitializeComponent”context@OmriTurgeman您可能还需要将Search.designer.cs中的类定义更改为泛型。已更新为包含sampleerror:使用泛型类型“System.Collections.generic.IList”的错误1需要1个类型arguments@OmriTurgeman我发布的代码编译得很好。确保使用的是IList
而不是IList
。您可能必须使用System.Collections将添加到文件顶部。
List<Foo> somelist = new List<Foo>();
Search search = new Search(somelist);
public partial class Search : Form
{
public Search(IList mylist)
{
}
}
public partial class Search<T> : Form
{
public Search(List<T> mylist)
{
}
}
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
}