C# 用于打开新表单C的类方法#
我想知道是否有其他方法在方法中执行下面的代码,因为我在几乎所有的表单中使用不同的表单实例调用此方法。 所以我需要创建一个代码,比如:C# 用于打开新表单C的类方法#,c#,.net,C#,.net,我想知道是否有其他方法在方法中执行下面的代码,因为我在几乎所有的表单中使用不同的表单实例调用此方法。 所以我需要创建一个代码,比如: public void NewForm(Form target) ... target myform = new target(); target.Show(); ... 把它叫做: NewForm(Form2); 谢谢你的代码和其他向我展示通用方法的人。我不知道这种通用方法 C类 public void NewForm<T>() where T:
public void NewForm(Form target)
...
target myform = new target();
target.Show();
...
把它叫做:
NewForm(Form2);
谢谢你的代码和其他向我展示通用方法的人。我不知道这种通用方法
C类
public void NewForm<T>() where T:Form, new()
{
T frm = new T(); ...
frm.Show();
}
public void NewForm(),其中T:Form,new()
{
T frm=新的T()。。。
frm.Show();
}
Form.cs
private void button1_Click_1(object sender, EventArgs e)
{
NewForm<Form2>();
}
private void按钮1\u单击1(对象发送者,事件参数e)
{
NewForm();
}
谢谢你的代码和其他向我展示通用方法的人。我不知道这种通用方法
C类
public void NewForm<T>() where T:Form, new()
{
T frm = new T(); ...
frm.Show();
}
public void NewForm(),其中T:Form,new()
{
T frm=新的T()。。。
frm.Show();
}
Form.cs
private void button1_Click_1(object sender, EventArgs e)
{
NewForm<Form2>();
}
private void按钮1\u单击1(对象发送者,事件参数e)
{
NewForm();
}
我不知道这样的方法有多有用,但有几种方法可以采用
通用方法
定义:
void NewForm<T>()
where T : Form, new()
{
T instance = new T();
instance.show();
}
void NewForm(Type formType)
{
if(formType.IsSubclassOf(typeof(Form)))
{
var form = Activator.CreateInstance(formType) as Form;
form.show();
}
}
void NewForm(Form form)
{
form.show();
}
调用:
NewForm<LayoutForm>();
NewForm(LayoutForm);
NewForm(new LayoutForm());
- 优点:
- 你要的是这种格式
- 缺点:
- 不允许将参数传递给构造函数
- 如果不能使用
Activator.CreateInstance(x)创建类型,则抛出代码>
- 运行时类型检查
void NewForm<T>()
where T : Form, new()
{
T instance = new T();
instance.show();
}
void NewForm(Type formType)
{
if(formType.IsSubclassOf(typeof(Form)))
{
var form = Activator.CreateInstance(formType) as Form;
form.show();
}
}
void NewForm(Form form)
{
form.show();
}
调用:
NewForm<LayoutForm>();
NewForm(LayoutForm);
NewForm(new LayoutForm());
- 优点:
- 允许传递参数
- 安全->编译时检查类型
- 缺点:
- 不是你要求的格式
- 有点无意义
void NewForm<T>()
where T : Form, new()
{
T instance = new T();
instance.show();
}
void NewForm(Type formType)
{
if(formType.IsSubclassOf(typeof(Form)))
{
var form = Activator.CreateInstance(formType) as Form;
form.show();
}
}
void NewForm(Form form)
{
form.show();
}
调用:
NewForm<LayoutForm>();
NewForm(LayoutForm);
NewForm(new LayoutForm());
- 优点:
- 你要的是这种格式
- 缺点:
- 不允许将参数传递给构造函数
- 如果不能使用
Activator.CreateInstance(x)创建类型,则抛出代码>
- 运行时类型检查
void NewForm<T>()
where T : Form, new()
{
T instance = new T();
instance.show();
}
void NewForm(Type formType)
{
if(formType.IsSubclassOf(typeof(Form)))
{
var form = Activator.CreateInstance(formType) as Form;
form.show();
}
}
void NewForm(Form form)
{
form.show();
}
调用:
NewForm<LayoutForm>();
NewForm(LayoutForm);
NewForm(new LayoutForm());
- 优点:
- 允许传递参数
- 安全->编译时检查类型
- 缺点:
- 不是你要求的格式
- 有点无意义
public void NewForm(),其中T:Form,new(){T frm=new T();…}
你在问如何按名称实例化表单吗?请看这里:当然。公开我很难找出代码中的变量,因为你只发布了一个实现。。。你能用简单的英语解释一下你的方法的不同实现之间有什么不同吗?public void NewForm(),其中T:Form,new(){T frm=new T();…}