1. 程式人生 > >Unity 通過基類找到所有繼承的子類

Unity 通過基類找到所有繼承的子類

忘了當時是為了幹啥了,不過感覺這個方法挺渣的,有需求可以參考下哦
/// <summary>
    /// 通過基類找到所有繼承的子類
    /// </summary>
    public void Info()
    {
        mConditions.Clear();
        mConConTypes.Clear();
        var types = Assembly.GetCallingAssembly().GetTypes();
        var aType = typeof(BaseCondition);
        foreach (var type in types)
        {
            var baseType = type.BaseType;  //獲取基類
            while (baseType != null)  //獲取所有基類
            {
                if (baseType.Name == aType.Name)
                {
                    Type objtype = Type.GetType(type.FullName, true);
                    object obj = Activator.CreateInstance(objtype);
                    if (obj != null)
                    {
                        BaseCondition info = obj as BaseCondition;
                        if (info.ConType != ConditionType.None)
                        {
                            SetCondition(info);
                        }
                    }
                    break;
                }
                else
                {
                    baseType = baseType.BaseType;
                }
            }
        }
    }