Unity客户端开发中遇到的问题

1.如何获取一个List<中的一个泛型>?

详细描述:我想获取List的泛型类型,然后强转List里面的实例。
网上找了很久都没有找到相关的资料,不知道是不是因为我这个想法比较愚蠢,总之比较好奇能不能这么做。
然后就产生了相关的问题

class Human
{
    public virtual void Eat() { }
    public virtual void Sleep() { }
    public virtual void Study() { }
}
class Man : Human
{
    public void ManFun() { }
}
class Woman : Human
{
    public void WomanFun() { }
}
public class Test : MonoBehaviour
{
    List<Human> HumanList = new List<Human>();
    void Start()
    {
        HumanList.Add(new Man());
        HumanList.Add(new Woman());
        HumanList.Add(new Mid());
        Debug.Log($"HumanListType:{HumanList.GetType()}\n{HumanList[0].GetType()}\n{HumanList[1].GetType()}");
        var rType = HumanList[0].GetType().BaseType;
        Debug.Log(rType);
        Debug.Log(HumanList[2].GetType().BaseType);

    }
}

1.获取HumanList类型,当我们打印List的类型时:
结果为:HumanListType:System.Collections.Generic.List`1[Human]
距离我想得到的Human类型又更近了,我想知道这个Human里面的相关信息还要拿到Human这个类型
2.当HumanList被new出来并且该List中有元素时的做法
我当时就想到了获取到实例然后再获取其父类的类型
结果为:Human
但是:该方法有个弊端,List<>在规定泛型的时候肯定是以BaseType做为泛型,如果继承的链条太长,就会导致套娃!
3.当HumanList仅仅只是声明的状态时,如何获取其泛型的类型
此时GetType会报NoneReference。

你可能感兴趣的:(工作日记,unity,c#,游戏引擎)