开发者

Cannot store an array of classes

I am trying to store an array of classes which I can initialise later on. I am trying to do it like so:

Type[] x = { className };

However, I am getting the following error:

'myNamespace.className' is a 'type' but it开发者_C百科 is used like a 'variable'

Any guidance would be appreciated.


Use the typeof operator, which is used to obtain a System.Typefor a type.

Type[] x = { typeof(className) };

EDIT:

In response to your comment: to create an instance of the type from a System.Type, use an appropriate overload of the Activator.CreateInstance method.

E.g.

Type t = typeof(int);

// Boxed Int32 with value 0
object o = Activator.CreateInstance(t);

In your case, since you know the base-type of the class, you can of course cast the return-value to the base-type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜