查看: 546|回复: 0
打印 上一主题 下一主题

[其他] 获得所有继承自BaseType的类

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53456
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-5-29 02:21:54 |只看该作者 |倒序浏览

获得所有继承自BaseType的类,并将他们的方法保存到字典中。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;

  6. public class BaseClass
  7. {
  8.         public string cName = "BaseClass";
  9.         public virtual string GetName()
  10.         {
  11.                 return cName;
  12.         }
  13. }
  14. public class NewClass1 : BaseClass
  15. {
  16.         private static string cName = "NewClass1";
  17.         public override string GetName()
  18.         {
  19.                 return cName;
  20.         }
  21. }
  22. public class NewClass2 : BaseClass
  23. {
  24.         private static new string cName = "NewClass2";
  25.         public override string GetName()
  26.         {
  27.                 return cName;
  28.         }
  29. }

  30. public class ClassTest : MonoBehaviour
  31. {
  32.         public delegate string GetEvent();
  33.         public Dictionary<string, GetEvent> dic = new Dictionary<string, GetEvent>();

  34.         void Init()
  35.         {
  36.                 Type[] arr = GetChildTypes(typeof(BaseClass));
  37.                 foreach(Type t in arr)
  38.                 {
  39.                         object obj = Activator.CreateInstance(t);
  40.                         BaseClass bc = obj as BaseClass;
  41.                         if (!dic.ContainsKey(bc.GetName()))
  42.                                 dic.Add(bc.GetName(), null);
  43.                         dic[bc.GetName()] += bc.GetName;
  44.                 }
  45.         }

  46.         // 获取继承 baseType 的所有类型
  47.         private Type[] GetChildTypes(Type baseType)               
  48.         {               
  49.                 List<Type> listType = new List<Type>();
  50.                 Assembly assem = Assembly.GetAssembly(baseType);       
  51.                 foreach (Type tChild in assem.GetTypes())
  52.                 {                       
  53.                         if (tChild.BaseType == baseType)                               
  54.                         {                               
  55.                                 listType.Add(tChild);                               
  56.                         }                       
  57.                 }               
  58.                 return listType.ToArray();               
  59.         }
  60. }
复制代码
最近在做服务器下发指令之后客户端对应做操作,每条新的指令都需要新建一个文件来写发送和接收。
接收方式是收到消息之后根据消息明,在保存的 <消息名, 消息回调> 字典里调用对应回调函数。
为了不要每次新增一个命令就手动写一条保存,而且也很可能会遗漏,因此做法就是所有命令类都继承 CmdBase , 并且在网络初始化的时候先
获取所有继承自 BaseClass,之后将回调保存到字典中

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-4 20:07 , Processed in 0.080436 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部