-2

I'd like to do like that in c#...

Type myDict = Dictionary<string, object>;
Type myDictList = List<myDict>;

It's kind of possible in typescript, but after looking hard and long, didn't find whether it's possible at all.

LinPy fan
  • 179
  • 1
  • 11
  • 2
    `Type myDict = typeof(Dictionary)`, and then Possible duplicate of [Pass An Instantiated System.Type as a Type Parameter for a Generic Class](https://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class) – GSerg May 10 '20 at 15:38
  • This works, thanks, @GSerg Can't understand how i didn't think of that myself :D – LinPy fan May 10 '20 at 17:49
  • And..., If you have an object of a particular Type you can get the type information by calling GetType: `Type someType = objectOfSomeType.GetType();` – Flydog57 May 10 '20 at 19:06
  • well... Actually what i looked for is called global type aliases(e.g. using MyTypeName = Dictionary;). these are valid only for current file and can't be exported unfortunately atm. I wanted that to shorten some names like List>, which i use a lot. – LinPy fan May 10 '20 at 19:11
  • You can always inherit from that ype. – Rotem May 10 '20 at 19:16
  • Just tried it. If i inherit from it, type casts won't work. Intellisense tells everything's ok, but it fails at runtime. Type casts are for unboxing(i'm writing json (de)serialization solution for some ScriptableObject unity entites. Unfortunately, can't be automated using json frameworks/packages, and writing such code in static type language is a pain :D – LinPy fan May 10 '20 at 19:31

1 Answers1

0

Well seems like what i was looking for were actually global type aliases. E.g. using MyType = List<Dictionary<string, MyOtherCustomType>>; to shorten the type name for more readable code.

Unfortunately, they're valid only per-file.

LinPy fan
  • 179
  • 1
  • 11