导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2019/2/13 14:50:57
标题:
Java有现成的类似Delphi object Dispatch的功能或实现思路么?唉,没用过Java
浏览:819
加入我的收藏
楼主:
如题,C#有实现类似功能的例子。感觉应该可以。有Java熟的大侠指点一二吧!
----------------------------------------------
-
作者:
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; }
----------------------------------------------
-