Data Construction Tool Class (2) Reflection Execution Method

Data Construction (2) Reflection Execution Method

Prerequisite: Knowing only the program name xxxx.exe
works for calling methods by string names

        /// <summary>
        /// Reflection call method entry handler
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public virtual string EventHander(string str)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => { this.EventHander(str); }));
            }
            var Info = p.Split ('|');
            var classInfo = Info [0] .Split ('?');
            var value = Info.Length > 1 && Info[1] != "undefined" && !string.IsNullOrEmpty(Info[1]) ? Info[1].Split('&') : null;
            var result = ExeMethod(classInfo, value);
            if (result == null)
            {
                return "";
            }
            if (result.Msg != null)
            {
                FormTools.ShowTip(result.Msg);
            }
            if (result.ResultType == ResultType.method)
            {
                return EventHander(result.Result.ToString());
            }
            return result.Result.ToString();
        }

        /// <summary>
        /// Execute the method and return the data
        /// </summary>
        public Results ExeMethod(string[] classInfo, string[] value)
        {
            try
            {
                //Get all types of the assembly
                var exeNames = Application.ExecutablePath.Replace(Application.StartupPath, "").Replace(@"\", "");
                var ass = Assembly.LoadFrom(exeNames);
                var mytypes = ass.GetTypes();
                foreach (var my in mytypes.Where(my => my.Name.ToLower().Equals(classInfo[0].ToLower() + "controller")))
                {
                    //Instantiate the object instance parameters
                    var objName = Activator.CreateInstance(my);

                    // execute method
                    var LoginForm = my.GetMethod(classInfo[1]);
                    return (Results) LoginForm.Invoke(objName, value);
                }
                throw new MessageTipShow("No related methods found:" + classInfo[0]);
            }
            catch (TargetInvocationException em)
            {
               throw em;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

transfer

var  action="Home?GetList";
var value="Expense"
EventHander(action + "|" + value)

If you dislike the native winform control, you can write this method, and then call this method on the html page to execute the front and back interactive call examples as follows

/**
 *
 * @param {} action backend controller and method name are linked with ?
 * @param {} value parameters are linked with &
 * @returns {} object
 */
function Hander(action, value) {
    return window.external.EventHander(action + "|" + value);
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325627392&siteId=291194637