Unable to save class array

I am trying to save an array of classes that I can initialize later. I am trying to do it like this:

Type[] x = { className };

However, I get the following error:

'myNamespace.className' is a type, but it is used as a "variable"

Any guidance would be appreciated.

+3
source share
1 answer

Use the operator that is used to get for the type. typeofSystem.Type

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

EDIT:

In response to your comment: System.Typeuse the appropriate overload to create an instance of the type from . Activator.CreateInstance

eg.

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, pass the return value to the base type.

+5
source

All Articles