Search Unity

Resolved How do you create a Dictionary with Type as the key?

Discussion in 'Scripting' started by ERN468, Feb 2, 2023.

  1. ERN468

    ERN468

    Joined:
    Apr 19, 2022
    Posts:
    1
    I tried this and it doesn't work:
    upload_2023-2-1_20-37-29.png

    When I hover over the red it says:
    upload_2023-2-1_20-40-36.png

    Why is a type not a valid key if the Dictionary takes Type as a key?

    Edit:
    Nvm I did typeof(Fish) and it worked.
    I swear I spent like 20 mins trying to fix this and I figured it out 3 mins after I post this.
     
    Last edited: Feb 2, 2023
    Bunny83 likes this.
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    System.Type
    is a class that belongs to the reflection system of C#. This class "describes" the structure of another type. What you try to store in the dictionary is a class itself which doesn't make sense to the compiler. A class is a concept and not a value that you could store somewhere. You need to use the typeof operator to get the System.Type object of a certain class.
    typeof(Fish)
    instead of
    Fish
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Just be careful whenever you're anywhere near serialization and Type. A type reference is really only good for the current application memory space and loaded scripts. If you save a Type, it does not save something that is guaranteed to work properly when you load it in the future, especially after assembly files change or with generics. You can as the Type itself to get a string that represents the type's name, full name, assembly full name, etc. Then you have [MovedFrom] attributes and other hacks. You can look for various solutions to this, usually people call it "SerializableType" or something like it. There's no real good plan here.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,939
    I don't imagine this to be a regular problem though as Unity can't serialise types? (Nor can it serialise dictionaries)

    That said renaming/moving types does cause issues with
    [SerializeReference]
    , which is where the
    [MovedFrom]
    attribute comes into play.
     
    Bunny83 likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    Since we already sidestepped the question a bit, If you're looking for a generally serializable System.Type reference, I made such a wrapper about 10 years ago. It's still on my dropbox... I need to move it somewhere more sensible in the future ^^.

    It uses a compact binary format which stores the type as a fully qualified string. Though it also does store generic arguments recursively. So it should support literally ANY concrete type. On top of that I also made a serializable MethodInfo which does exactly that. It uses the serializable type for the parent type of the method as well as for the arguments of the method in order to uniquely identify the method in case of overloads.

    You barely would need that. Just like my attempt in storing coroutines to disk. It actually works, with some limitations. You can do some crazy things with reflection, but in the end you have to ask yourself if it's worth it and how easily it can break.