Simple UI framework | 6. Develop dictionary extension class

Simple UI Framework

Develop dictionary extension class

Dictionary extension class

At present, we need two steps to get an item in the dictionary. The first step is to define the type of the item, and the second step is TryGetValue

        BasePanel panel;
        panelDict.TryGetValue(panelType, out panel);

This is cumbersome, so let's create an extension class next.
Create a new script DictionaryExtension dictionary extension class.

public static class DictionaryExtension
{
    
    

}

The dictionary class is a built-in class of the system. When we extend it, we must first declare it as static, and the extended class must be a static method.
We want to define a method and get something, so we need to return a value, but we don't know what the return value type is, so we define it as void first .

public static void TryGet<Tkey,Tvalue>(this Dictionary<Tkey,Tvalue> dict,Tkey key)
{
    
    

}

The TryGet method is used to try to get the value according to the key , and return the value directly if it is obtained , and return null directly if it is not obtained . First, there is a this object, which represents the defined dictionary object. We don't know the type of dictionary here, so here we declare two generic types for this method

TryGet<Tkey,Tvalue>

So the dictionary type is

this Dictionary<Tkey,Tvalue> dict

The TryGet method we declared is called for a dictionary object, which we don’t know now. If this method is directly defined in the dictionary, we can directly get the current object through this . But now our extension method is defined outside the dictionary, now we can declare such a parameter, which represents the object when we call the method, that is, this dictionary represents the dictionary we want to get the value of.
Next, we need to get the value through the Key , here we define it as

Tkey key

Next inside the TryGet

public static void TryGet<Tkey,Tvalue>(this Dictionary<Tkey,Tvalue> dict,Tkey key)
{
    
    
        Tvalue value;
        dict.TryGetValue(key, out value);
        return value;
}

First define a value , then call the TryGetValue method to get the value, return it directly , return the value directly if you get it , and return null if you don't get it .
So now we know what type the return value is, which is Tvalue type, so the code in our entire extension class is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class DictionaryExtension
{
    
    

    public static Tvalue TryGet<Tkey,Tvalue>(this Dictionary<Tkey,Tvalue> dict,Tkey key)
    {
    
    
        Tvalue value;
        dict.TryGetValue(key, out value);
        return value;
    }
}

Next we go back to the UIManager script.
So we can call this extension method directly.

        //BasePanel panel;
        //panelDict.TryGetValue(panelType, out panel);

        BasePanel panel = panelDict.TryGet(panelType);

Just pass the key directly to the calling method , and the key is panelType .
The next if statement can also be changed accordingly.

if (panel == null)
        {
    
    
            //string path;
            //panelPathDict.TryGetValue(panelType, out path);
            string path = panelPathDict.TryGet(panelType);
            GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
            instPanel.transform.SetParent(canvasTransform);
            panelDict.Add(panelType, instPanel.GetComponent<BasePanel>());
            return instPanel.GetComponent<BasePanel>();
        }
        else
        {
    
    
            return panel;
        }

There is a question, isn't TryGet here a generic? Why is there no need to specify the generic type here?
Because we call it through a dictionary object, the Key and Value of the dictionary object are already determined, so when you call the TryGet method through the dictionary object, the Tkey and Tvalue are determined according to the Key and Value of the dictionary object , this dictionary object is equivalent to the this object in the TryGet method parameter , so this this object does not require us to assign a value.

Summarize

Today we created a dictionary extension class for our future use.
The definition of the extended class is also the syntax that comes with C#.
Three requirements for the extension method:
1. The class that declares the extension method must be declared as static;
2. The extension method itself must be declared as static;
3. The keyword this must be included before the first parameter type of the extension method,
and the extension method can also be Used with generics.

Guess you like

Origin blog.csdn.net/m0_64058685/article/details/124569596