Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

c# - Get error when comparing typeof(Dictionary), because Dictionary`2

I`m working in Unity, and in my game, I want to get variables of objects to give player permission to change them. So, this function do this:

public static void GetVariables(GameObject objectToInspect, GameObject configPanel)
    {
        configPanel.GetComponent<Grid>().enabled = false;
        Component[] objectComponents = objectToInspect.GetComponents(typeof(Component));
        foreach (var component in objectComponents)
        {
            foreach (var property in component.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.IsDefined(typeof(SerializeField), true))
                {
                    try
                    {
                        int id = types[property.PropertyType];
                        switch (id)
                        {
                            case 0:
                                {
                                    GameObject numberChooser = Instantiate(UIs[4], configPanel.transform);
                                    numberChooser.GetComponent<Chooser>().property = property;
                                    numberChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 1:
                                {
                                    GameObject numberChooser = Instantiate(UIs[4], configPanel.transform);
                                    numberChooser.GetComponent<Chooser>().property = property;
                                    numberChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 2:
                                {
                                    GameObject arrayChooser = Instantiate(UIs[0], configPanel.transform);
                                    arrayChooser.GetComponent<Chooser>().property = property;
                                    arrayChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 3:
                                {
                                    GameObject arrayChooser = Instantiate(UIs[0], configPanel.transform);
                                    arrayChooser.GetComponent<Chooser>().property = property;
                                    arrayChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 4:
                                {
                                    GameObject arrayChooser = Instantiate(UIs[0], configPanel.transform);
                                    arrayChooser.GetComponent<Chooser>().property = property;
                                    arrayChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 5:
                                {
                                    GameObject vectorChooser = Instantiate(UIs[6], configPanel.transform);
                                    vectorChooser.GetComponent<Chooser>().property = property;
                                    vectorChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 6:
                                {
                                    GameObject booleanChooser = Instantiate(UIs[2], configPanel.transform);
                                    booleanChooser.GetComponent<BoolChooser>().property = property;
                                    booleanChooser.GetComponent<BoolChooser>().component = component;
                                    break;
                                }
                            case 7:
                                {
                                    GameObject spriteChooser = Instantiate(UIs[5], configPanel.transform);
                                    spriteChooser.GetComponent<Chooser>().property = property;
                                    spriteChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            case 8:
                                {
                                    GameObject colorChooser = Instantiate(UIs[3], configPanel.transform);
                                    colorChooser.GetComponent<Chooser>().property = property;
                                    colorChooser.GetComponent<Chooser>().component = component;
                                    break;
                                }
                            default:
                                {
                                    Debug.Log(property.PropertyType.Name);
                                    break;
                                }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }
                }
            }
        }
        configPanel.GetComponent<Grid>().enabled = true;
        configPanel.GetComponent<Grid>().OnTransformChildrenChanged();
    }

There`s dictionary with types and integers:

public static Dictionary<Type, int> types = new Dictionary<Type,int>();
types[typeof(Single)] = 0;
    types[typeof(Int32)] = 1;
    types[typeof(Int32[])] = 2;
    types[typeof(List<Type>)] = 3;
    types[typeof(Dictionary<int, int[]>)] = 4;
    types[typeof(Vector3)] = 5;
    types[typeof(Boolean)] = 6;
    types[typeof(Sprite)] = 7;
    types[typeof(Color)] = 8;

But I have a problem with Dictionaries and Lists. System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <437ba245d8404784b9fbab9b439ac908>:0

This try to check "Dictionary`2" instead of "Dictionary<int, int[]>". How I can get Dictionary without "`2" and List without "`1/2/3" at the end?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...