类参考 [英] Class reference
问题描述
如何创建一个将类(不是实例)作为
参数的方法?
我希望此参数只接受Windows表单类,即继承System.Windows.Forms.Form的
类。
谢谢!
- -
Erick Sasse
How can I make a method that takes a class (not an instance) as
parameter?
And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows.Forms.Form.
Thanks!
--
Erick Sasse
推荐答案
你不能。课程本身不做任何事情。你只能通过
类的实例。
你想做什么?
" Erick Sasse" < ES *********** @ nospam.yahoo.com.br>在消息中写道
新闻:#K ************** @ TK2MSFTNGP10.phx.gbl ...
You cannot. Classes by themselves do not do anything. You can only pass
instances of classes.
What is it that you want to do?
"Erick Sasse" <es***********@nospam.yahoo.com.br> wrote in message
news:#K**************@TK2MSFTNGP10.phx.gbl...
我该如何制作将类(不是实例)作为
参数的方法?
我希望此参数只接受Windows Form类,即继承System.Windows的类。 Forms.Form。
谢谢!
- Erick Sasse
How can I make a method that takes a class (not an instance) as
parameter?
And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows.Forms.Form.
Thanks!
--
Erick Sasse
Peter Rilling写道:
Peter Rilling wrote:
你不能。课程本身不做任何事情。你只能通过课程实例。
你想做什么?
You cannot. Classes by themselves do not do anything. You can only
pass instances of classes.
What is it that you want to do?
我想创建一个接收表单类的方法,创建一个实例
,配置一些属性,然后显示创建的表单。
-
Erick Sasse
I want to create a method that receives a form class, create a instance
of it, configure some properties and then show the created form.
--
Erick Sasse
Erick Sasse写道:
Erick Sasse wrote:
如何创建一个接受类的方法(不是实例) )作为
参数?
我希望这个参数只接受Windows Form类,即继承System.Windows.Forms.Form的类。
How can I make a method that takes a class (not an instance) as
parameter?
And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows.Forms.Form.
你不能; C#不是德尔福。
你可以做的是拿一个Type参数,并传递typeof(MyForm)。
你不能得到编译器只接受typeof()表格
后代;你必须做运行时类型检查,比如
public void FormFactory(Type TypeType)
{
if(! typeof(Form).IsAssignableFrom(FormType))
抛出新的ArgumentException(
" FormType不是typeof()a Form descendant);
// yadda yadda
}
-
www.midnightbeach.com
这篇关于类参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!