DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: wjy13061029975
今日帖子: 7
在线用户: 23
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2019/2/13 14:50:57
标题:
Java有现成的类似Delphi object Dispatch的功能或实现思路么?唉,没用过Java 浏览:819
加入我的收藏
楼主: 如题,C#有实现类似功能的例子。感觉应该可以。有Java熟的大侠指点一二吧!
----------------------------------------------
-
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2019/2/13 15:13:43
1楼: C#的例子
----------
    public class CommandHandler : System.Attribute
    {
        /// <summary>
        /// 命令类型
        /// </summary>
        public string CommandType;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="commandType">命令类型</param>
        public CommandHandler(string commandType) { this.CommandType = commandType; }
    }

----------

/// <summary>
    /// 命令派发类
    /// </summary>
    public class CommandDispatcher
    {
        #region 定义并获取方法字典
        /// <summary>
        /// 获得消息和消息对应处理方法的RTTI信息,并调用相应的方法
        /// </summary>
        private class CommandDispatcherDictionary
        {
          private readonly Dictionary<string, System.Reflection.MethodInfo> _dict;

          /// <summary>
          /// 处理命令
          /// </summary>
          /// <param name="onInstance"></param>
          /// <param name="command"></param>
          /// <returns></returns>
          public bool HandleMessage(object onInstance, ref ICommand command)
          {
          System.Reflection.MethodInfo method;
          if (_dict.TryGetValue(command.Name, out method))
          {
          // 设置参数,并调用方法执行
          object[] p = new object[1];
          p[0] = command;
          return (bool)method.Invoke(onInstance, p);
          // CommandType = p[0];
          }
          else
          {
          return false;
          }
          }

          /// <summary>
          /// 构造处理方法字典
          /// </summary>
          /// <param name="t"></param>
          public CommandDispatcherDictionary(Type t)
          {
          _dict = new Dictionary<string, System.Reflection.MethodInfo>();
          foreach (var method in t.GetMethods())
          {
          var attribs = method.GetCustomAttributes(typeof(CommandHandler), true);
          if (attribs.Length > 0 && method.ReturnParameter != null)
          {
          // 检测返回值
          if  (method.ReturnParameter.ParameterType != typeof(bool)) 
          throw new Exception(string.Format("{0} 方法必须返回bool值", method.Name));

          // 检查方法的参数数量和类型
          var param = method.GetParameters();
          if (param.Length != 1) 
          throw new Exception(string.Format("{0} 方法只允许有一个参数", method.Name));
          // 检查是否是引用型参数(可以提供命令的修改)
          if (!param[0].ParameterType.IsByRef)
          throw new Exception(string.Format("{0} 方法参数必须为ref类型,并支持ICommand接口", method.Name, param[0].ParameterType.ToString()));

          // 添加合乎要求的方法到字典中
          _dict.Add(((CommandHandler)attribs[0]).CommandType, method);
          }
          }
          }
        }

        #endregion 

        // 命令处理方法字典
        private static readonly Dictionary<Type, CommandDispatcherDictionary> Dict;

        /// <summary>
        /// 创建并返回命令字典
        /// </summary>
        static CommandDispatcher()
        {
          Dict = new Dictionary<Type, CommandDispatcherDictionary>();
        }



        /// <summary>
        /// 派发消息函数
        /// </summary>
        /// <param name="objInstance">接受消息的对象</param>
        /// <param name="command">命令</param>
        /// <returns>是否处理消息</returns>
        public static bool Dispatch(object objInstance, ref ICommand command)
        {
          if (objInstance == null) return false;
          else
          {
          CommandDispatcherDictionary perType;
          lock (Dict)
          {
          if (!Dict.TryGetValue(objInstance.GetType(), out perType))
          {
          perType = new CommandDispatcherDictionary(objInstance.GetType());
          Dict.Add(objInstance.GetType(), perType);
          }
          }
          return perType.HandleMessage(objInstance, ref command);
          }
        }

    }

----------
实际使用时
private void ViewDoSetCurrentSelectedUnitSystem(object sender, MethodParameterArgs e)
        {
          ICommand command = new Command(CommandType.GetCurrentItem,  new GetCurrentItemCommand(){ItemType =ItemType.System,Item = e.Parameters[0]});
          CommandDispatcher.Dispatch(objectAAA, ref command);
        }

然后objectAAA对象如果存在类似下面的方法的话,下面的方法就会被执行
[CommandHandler(CommandType.GetCurrentItem)]
public bool GetCurrentItemInfo(ref ICommand command)
        {
          GetCurrentItemCommand value = (GetCurrentItemCommand) command.Value;
          switch (value.ItemType)
          {
          ......

          }
          return true;
        }
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行97.65625毫秒 RSS