/// <summary> /// 调用C#脚本函数。 /// </summary> /// <param name="type">对象类型。</param> /// <param name="method">方法名称。</param> /// <param name="args">调用参数。</param> /// <returns>脚本执行结果。</returns> public object Invoke(string type, string method, params object[] args) { if (this.m_Assembly == null) { if (this.CompileException != null) { throw this.CompileException; } else { throw new ScriptException("Error: An exception occurred while parsing the script block", null); } } InvodeContext ic = this.GetMethodContext(type, method); if (ic.Exception != null) { throw ic.Exception; } return(ic.Handler(ic.Method, args)); }
InvodeContext GetMethodContext(string type, string method) { // 生成Key。 string key = string.Format("{0}.{1}", type, method); InvodeContext ic = null; if (!this.m_Contexts.TryGetValue(key, out ic)) { ic = new InvodeContext(); ic.Type = type; ic.Name = method; // 求方法,求类型。 System.Type xType = null; if (!string.IsNullOrEmpty(type)) { xType = this.m_Assembly.GetType(type); if (xType != null) { ic.Method = xType.GetMethod(method); } else { ic.Exception = new ScriptException(string.Format("Error: type {0} not find", type), null); } } else { Module[] mods = this.m_Assembly.GetModules(false); var types = mods[0].GetTypes().Where(x => x.IsPublic).ToList(); foreach (Type vType in types) { ic.Method = vType.GetMethod(method); if (ic.Method != null) { xType = vType; break; } } } // 判断。 if (ic.Method == null && ic.Exception == null) { ic.Exception = new ScriptException(string.Format("Error: method {0} not find", method), null); } // 生成实例。 if (!ic.Method.IsStatic) { ic.Instance = System.Activator.CreateInstance(xType); } // 生产委托 ic.Handler = FastInvoker.CreateMethodInvoker(ic.Method); this.m_Contexts.Add(key, ic); } return(ic); }