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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With