Unity中对C#泛型<T>的使用

比较简单就不啰嗦了,直接上段小代码,清晰明了!


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

public class T_test : MonoBehaviour {

    private void Start()
    {
        Test<int>();
        Debug.Log("-------------");
        Test<string>();
    }

    private void Test<T>()
    {
        if (Types.Equals(typeof(T),typeof(int)))
        {
            Debug.Log("调用的类型是  int");
        }
        else if (Types.Equals(typeof(T), typeof(string)))
        {
            Debug.Log("调用的类型是  string");
        }
    }
}

运行结果:






猜你喜欢

转载自blog.csdn.net/u010133610/article/details/59098591