类的数组 [英] Array of classes

查看:58
本文介绍了类的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储类的列表,而不是类的实例。我计划使用一个数组,但是可以使用一个集合或其他形式,如果更多

合适。


我遇到的问题是我找不到我需要的C#语法

引用类类型。


public< class type> [] PowerTour = new< class type> [] {form1,form2};


然后我会访问它以获取一个类列表,然后我将

通过调用new来创建。


在Delphi中,您可以使用class of进行创建。构造。我该如何做?

C#?


-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是一种反击的艺术形式

I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more
appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.

public <class type>[] PowerTour = new <class type>[] {form1, form2};

Then later I will access this to get a list of classes which I will then
create with a call to new.

In Delphi you can do this with the "class of" construct. How can I do this in
C#?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

推荐答案

Chad Z. Hower aka Kudzu< cp ** @ hower.org>写道:
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
我需要存储类的列表,而不是类的实例。我曾计划使用数组,但如果更合适,可以使用集合或其他形式。

我遇到的问题是我找不到C#语法我需要引用类类型。
I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more
appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.




Just System.Type,并使用typeof运算符:


键入[] foo = new Type [] {typeof(object),typeof(System.Windows.Forms)};





要查找特定实例的类型,请在该实例上调用GetType。


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Just System.Type, and use the typeof operator:

Type[] foo = new Type[] {typeof(object), typeof(System.Windows.Forms)};

etc.

To find the type of a specific instance, call GetType on that instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


假设你有:

class myclass

{

public int f = 0;

}


和其他某个地方你写的课:


myclass [] mc = new myclass [5]; //创建了5个引用变量,它们可以指向5个实例/类型为myclass的对象

for(int i = 0; i< mc.Length; i ++)

{

mc [i] = new myclass();

mc [i] .f = i;

}
Suppose you have a :
class myclass
{
public int f=0;
}

and somewhere in someother class you write:

myclass [] mc= new myclass[5]; //5 reference variables created which can
point to 5 instances/objects of type myclass
for (int i=0; i<mc.Length ;i++)
{
mc[i]= new myclass();
mc[i].f= i;
}
public< class type> [] PowerTour = new< class type> [] {form1,form2};
form1和form2是你不会写的,因为这会在创建期间初始化

数组,这是你不想要的,因为:

然后稍后我将访问它以获取一个类列表,然后我将通过调用new来创建它们。



" Chad Z. Hower aka Kudzu" < CP ** @ hower.org>在消息中写道

news:Xn ****************** @ 127.0.0.1 ...我需要存储一个类列表,而不是类的实例。我曾计划使用数组,但如果
更合适,可以使用集合或其他形式。

我遇到的问题是我找不到C#语法我需要引用一个类类型。

public< class type> [] PowerTour = new< class type> [] {form1,form2};

在Delphi中,您可以使用class of来完成此操作。构造。我怎么能用C#做这个


- 乍得Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/
编程是一种反击的艺术形式
public <class type>[] PowerTour = new <class type>[] {form1, form2}; the form1 and form2 is what you''ll not write because that will intialize the
array during creation and which is what you dont want because:
Then later I will access this to get a list of classes which I will then
create with a call to new.

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1... I need to store a list of classes, not the instances of the classes. I had
planned to use an array, but can use a collection or some other form if more appropriate.

The problem I am having is that I cannot find the C# syntax I need to
reference a class type.

public <class type>[] PowerTour = new <class type>[] {form1, form2};

Then later I will access this to get a list of classes which I will then
create with a call to new.

In Delphi you can do this with the "class of" construct. How can I do this in C#?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"



Jon Skeet [C#MVP]< sk *** @ pobox.com>写在

新闻:MP ************************ @ msnews.microsoft.c om:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
只需System.Type,并使用typeof运算符:

类型[] foo = new Type [] {typeof(object),typeof(System.Windows.Forms)};


谢谢。有没有办法声明我的基类类型?或者只输入

generic?这是在德尔福我能做到的:


类型

TMyClassType = TMyClass类;


然后我可以使用TMyClassType,而不仅仅是基本类

TObject。这样做,所以当我把它们拉出来时我不需要输入演员

阵列。


看来因为使用了typeof类型在

中实现,copmiler在C#中有很大的不同。

要查找特定实例的类型,请在该实例上调用GetType。
Just System.Type, and use the typeof operator:

Type[] foo = new Type[] {typeof(object), typeof(System.Windows.Forms)};
Thanks. Is there any way to declare a type of my base class? Or is type
generic only? That is in Delphi I can do:

type
TMyClassType = class of TMyClass;

Then I can make an arry af TMyClassType, instead of just the basic class of
TObject. This makes it so I dont have to type cast when I pull them back out
of the array.

It appears though because of the use of typeof that types are implemented in
the copmiler quite a bit different in C#.
To find the type of a specific instance, call GetType on that instance.




将此文件提交以备将来使用。 ;)


-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是一种反击的艺术形式 ;



Will file this for future use too. ;)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


这篇关于类的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆