C# Type 的操作

1.根据type new一个新实例

public static object CreateReflect(Type type)
        {
            return Activator.CreateInstance(type);
        }  

2.根据type查找出所有继承了指定基类的子类

public static Dictionary<string, Type> LoadScript(Type _type)
        {
            Dictionary<string, Type> list = new Dictionary<string, Type>();
            var types = Assembly.GetCallingAssembly().GetTypes();
            //var aType = typeof(IBuffControl);
            var aType = _type;
            foreach (var type in types)
            {
                var baseType = type.BaseType;  //获取基类
                while (baseType != null)  //获取所有基类
                {
                    //Debug.Log(baseType.Name);
                    if (baseType.Name == aType.Name)
                    {
                        Type objtype = Type.GetType(type.FullName, true);
                        //object obj = Activator.CreateInstance(objtype);
                        //if (obj != null)
                        //{
                        //SeekTarget info = obj as SeekTarget;
                        list.Add(objtype.Name, objtype);
                        //}
                        break;
                    }
                    else
                    {
                        baseType = baseType.BaseType;
                    }
                }

            }
            return list;
        }

使用方法:LoadScript(typeof(BuffBase)); 

参考链接:https://www.cnblogs.com/nele/p/7689186.html

posted @ 2020-11-11 14:32  大刀乱飞  阅读(684)  评论(0)    收藏  举报