/// <summary>
        /// 单播,根据指令,通过反射创建一个对象,返回该处理方, 并默认不传递消息;
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="name"></param>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static bool Dispose(JsonData jsonData, string className = null, bool order = false)
        {
            if (className == null)
            {
                className = jsonData.FirstKey().ToLower();
            }

            IInstructionDispose dispose = FindDispose(className);

            if (!order && dispose != null)
            {
                return(dispose.Dispose(jsonData));
            }
        /// <summary>
        ///  如果 key 类不存在,则创建 key 类,如果存在,则收集其方法;
        ///
        ///  判断该方法是否包含该 mothedName;
        /// </summary>
        /// <param name="key"></param>
        /// <param name="mothedName"></param>
        /// <returns></returns>
        public static bool CreateDispose(string className, string mothedName)
        {
            // 在组件内部反射响应的类,如果没有找到,不抛异常;进而调用客户端注册的方法;

            // 可以加一个配置文件,以读取相应的配置文件,找对应的类;TODO

            string functionName = mothedName.ToLower();

            IInstructionDispose dispose = FindDispose(className);

            if (dispose != null)
            {
                return(dispose.Dispose(functionName));
            }

            Type type = Type.GetType(fullName + "." + className, false, true);

            if (type == null)             // 调用外部的函数进行处理;
            {
                return(CustomFunctionLibrary <JsonData> .ExternalDispose(className) || CustomFunctionLibrary <JsonData> .ExternalDisposeA(functionName));
            }

            try
            {
                dispose = ((IInstructionDispose)type.Assembly.CreateInstance(type.FullName, true));
            }
            catch (InvalidCastException e)
            {
                e = new InvalidCastException("没有实现 IInstructionDispose 接口!");

                throw e;
            }

            // 将处理类缓存起来;

            dicts.Add(className.ToLower(), dispose);

            // 下面的方法是判断方法是否存在,并不是正真的处理;

            return(dispose.Dispose(functionName));
        }
Exemple #3
0
        /// <summary>
        /// 单播,根据指令,通过反射创建一个对象,返回该处理方, 并默认不传递消息;
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="name"></param>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static bool CreateDispose(JsonData jsonData = null, string name = null)
        {
            // 这里因为 IDictionary 的 key 和 value 都是 ICollection ,所以只能这样遍历;

            if (name == null)
            {
                name = jsonData.FirstKey();
            }

            IInstructionDispose dispose = FindDispose(name);

            if (dispose != null)
            {
                return(dispose.Dispose(jsonData));
            }


            // 在组件内部反射响应的类,如果没有找到,不抛异常;进而调用客户端注册的方法;

            // 可以加一个配置文件,以读取相应的配置文件,找对应的类;TODO

            // 这里先调用客户端注册的方法;

            Type type = Type.GetType(fullName + "." + name, false, true);

            if (type == null)
            {
                Func <JsonData, bool> func = CustomFunctionLibrary <JsonData> .GetFunc(name);

                Action action = CustomFunctionLibrary <JsonData> .GetAction(name);

                if (func != null)
                {
                    return(func(jsonData));
                }
                if (action != null)
                {
                    action();

                    return(true);
                }

#if UNITY_EDITOR
                throw new NullReferenceException("没有找到指定的类");
#endif
                return(false);                // 不抛异常
            }

            try
            {
                dispose = ((IInstructionDispose)type.Assembly.CreateInstance(type.FullName, true));
            }
            catch (InvalidCastException e)
            {
                e = new InvalidCastException("没有继承 CommandDisposeBase 类");

                throw e;
            }

            dicts.Add(name, dispose);

            return(dispose.Dispose(jsonData));
        }