0

Is this not what Type is able to offer? Is there another way I can achieve the below?

Type t = typeof(Int32);
List<t> list = new List<t>();

This seems valid usage to me based on what the docs mentioned:

Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types

But I have a feeling this is a better description which means that I cannot use the class Type as above as it seems to only hold information about the type:

Type describes data types. It stores type information in a variable, property or field. The Type class represents the program's metadata, which is a description of its structure but not the instructions that are executed.

I was trying to use the above in order to solve a problem where I have a 'List' where t is a user defined type, that list gets stored within a field of type object I will at some point later on need to parse it back to it's original type, I was hoping that the class Type will allow me to retain the type for parsing back to the original later on.

I am after something like this:

public IList ObjectToList(Type t, object o)
{
    return new List<t>(o);
}
Abs
  • 165
  • 1
  • 14
  • 1
    What are you trying to do in the first place? Generics mean you *don't* need to specify the type. Just as `List` can be used to create lists of any type, `MakeList=>new List();` can do the same – Panagiotis Kanavos Dec 19 '18 at 15:16
  • Why do you believe `Represents type declarations` should be an description for type can be used to create a type of array/list? - A type is a `reprenstation` or the various possible types so `Type t = typeof(List);` will `represent` the `Type` of a `List` – Rand Random Dec 19 '18 at 15:17
  • Try this: `List list = new List();` – Jim Fell Dec 19 '18 at 15:17
  • @Abs This looks like a case of the XY Problem. You have a problem with X and think Y is the answer. When you encounter problems with Y, you ask about Y, not X. What is the *actual* problem you want to solve? – Panagiotis Kanavos Dec 19 '18 at 15:19
  • @OwenPauling This isn't a duplicate because the problem I am trying to address here is to convert an existing list stored within an `object` back to a list of the correct type while the marked question creates a new list from a `Type`, my edit explains this, not sure if you've seen it before marking as a duplicate – Abs Dec 19 '18 at 15:58
  • Here's your method: ```IList ObjectToList(object obj) => obj as List ?? new List();``` – Michael Puckett II Dec 19 '18 at 16:20
  • Here's your list: ```var persons = new List() { new Person("Mathew", "Mark"), new Person("Luke", "John") };``` – Michael Puckett II Dec 19 '18 at 16:20
  • Here's your casting: ```object obj = persons; var list = ObjectToList(obj);``` – Michael Puckett II Dec 19 '18 at 16:21
  • ```foreach (var person in list) Console.WriteLine(person.First);``` – Michael Puckett II Dec 19 '18 at 16:21

2 Answers2

1

It isn't a valid usage because it will not compile. The Type class represents a type declaration. Type t = typeof(Int32); will give you the System.Type object for Int32 and assign to the variable t. It cannot actually be used as a type.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
0

You cannot use Runtime variable as Type identifier.

Type t = typeof(Int32);

actually holds type of Int32 inside variable t


you can use it

List<Int32> list = new List<Int32>();   
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72