internal void HookupHandler(IBuildProvider provider, ScriptScope moduleGlobals, object self, object target) { DynamicFunction scriptFunction = new DynamicFunction((object)moduleGlobals.GetVariable(_handlerName)); Delegate handler = EventHandlerWrapper.GetWrapper( provider, self, scriptFunction, _scriptVirtualPath, typeof(EventHandler)); _eventInfo.AddEventHandler(target, handler); }
internal static EventHookupHelper Create(Type type, string eventName, string handlerName, DynamicFunction f, string scriptVirtualPath) { EventInfo eventInfo = GetEventInfo(type, eventName, f, scriptVirtualPath); if (eventInfo == null) return null; return new EventHookupHelper(handlerName, eventInfo, scriptVirtualPath); }
internal static EventInfo GetEventInfo(Type type, string eventName, DynamicFunction f, string scriptVirtualPath) { EventInfo eventInfo = type.GetEvent(eventName); // If it doesn't match an event name, just ignore it if (eventInfo == null) return null; return eventInfo; }
public static Delegate GetWrapper(IBuildProvider provider, DynamicFunction f, string virtualPath, Type delegateType) { // Get the MethodInfo only once if (_handlerMethodInfo == null) { _handlerMethodInfo = typeof(EventHandlerWrapper).GetMethod("Handler"); } EventHandlerWrapper wrapper = new EventHandlerWrapper(provider, f, virtualPath); // Create a delegate of the required type return Delegate.CreateDelegate(delegateType, wrapper, _handlerMethodInfo); }
public object CallDataBindingFunction(DynamicFunction f, params object[] args) { return CallDataBindingFunction((object)null, f, args); }
public object CallFunction(DynamicFunction f, params object[] args) { return EngineHelper.CallMethod(_scriptEngine, f, _scriptVirtualPath, args); }
internal void HookupControlEvent(Control control, string eventName, string handlerName, int line) { EventInfo eventInfo = control.GetType().GetEvent(eventName); if (eventInfo == null) { throw new Exception("Control '" + control.ID + "' doesn't have an event named '" + eventName + "'"); } object o = null; if (_scopeDictionary == null || !_scopeDictionary.TryGetValue(handlerName, out o)) { Misc.ThrowException("The page doesn't have an event handler named '" + handlerName + "'", null, _templateControl.AppRelativeVirtualPath, line); } DynamicFunction handlerFunction = new DynamicFunction(o); Delegate handler = EventHandlerWrapper.GetWrapper(this, _templateControl, handlerFunction, _scriptVirtualPath, eventInfo.EventHandlerType); eventInfo.AddEventHandler(control, handler); }
public void SetDynamicFunction(DynamicFunction f, string virtualPath) { _f = f; _virtualPath = virtualPath; }
public EventHandlerWrapper(IBuildProvider provider, DynamicFunction f, string virtualPath) { _f = f; _virtualPath = virtualPath; _provider = provider; }
internal abstract bool ProcessEventHandler(string handlerName, Type type, DynamicFunction f);
private void RenderMethod(HtmlTextWriter writer, Control container) { ScriptTemplateControl scriptTemplateControl = ScriptTemplateControl.GetScriptTemplateControl(container); // Get the compiled code for the render method logic CompiledCode compiledCode = scriptTemplateControl.GetSnippetRenderCode(container.UniqueID, this); // Execute it in our module EngineHelper.ExecuteCompiledCode(compiledCode, scriptTemplateControl.ScriptModule); // We should always find our render function in the module // REVIEW: we shouldn't have to do this, and should instead work with a lambda like mechanism (bug 218654) object f = scriptTemplateControl.ScriptModule.GetVariable(RenderMethodName); Debug.Assert(f != null); // Call the render method DynamicFunction renderFunction = new DynamicFunction(f); EngineHelper.CallMethod(scriptTemplateControl.ScriptEngine, renderFunction, null /*defaultVirtualPath*/, new SnippetRenderHelper(writer, container.Controls)); }
private void CallFunction(ScriptEngine engine, DynamicFunction f) { if (f == null) return; try { f.Invoke(engine); } catch (Exception e) { if (!EngineHelper.ProcessRuntimeException(engine, e, ScriptVirtualPath)) throw; } }
internal override bool ProcessEventHandler(string handlerName, Type type, DynamicFunction f) { // Does it look like a handler? if (!handlerName.StartsWith(HandlerPrefix)) return false; // Handle the special pseudo-events if (String.Equals(handlerName, "Application_OnStart", StringComparison.OrdinalIgnoreCase) || String.Equals(handlerName, "Application_Start", StringComparison.OrdinalIgnoreCase)) { _onStartMethod = f; return true; } else if (String.Equals(handlerName, "Application_OnEnd", StringComparison.OrdinalIgnoreCase) || String.Equals(handlerName, "Application_End", StringComparison.OrdinalIgnoreCase)) { _onEndMethod = f; return true; } else if (String.Equals(handlerName, "Session_OnEnd", StringComparison.OrdinalIgnoreCase) || String.Equals(handlerName, "Session_End", StringComparison.OrdinalIgnoreCase)) { // REVIEW: can we support Session_End? throw new Exception("Session_End is not supported!"); } string eventName = handlerName.Substring(HandlerPrefix.Length); // This will throw if the event doesn't exist EventHookupHelper.GetEventInfo(type, eventName, f, ScriptVirtualPath); if (_eventHandlers == null) _eventHandlers = new Dictionary<string, DynamicFunction>(); _eventHandlers[eventName] = f; return true; }
private void InitMethodsInternal(Type type, ScriptScope moduleGlobals) { // If CompiledCode is null, there was no script to compile if (CompiledCode == null) return; foreach (KeyValuePair<string, object> pair in moduleGlobals.GetItems()) { // Wrap it as a dynamic object. It may not be something callable, and if it's not, // it will fail when we try to call it. DynamicFunction f = new DynamicFunction(pair.Value); ProcessEventHandler(pair.Key, type, f); } }
public object CallDataBindingFunction(object dataItem, DynamicFunction f, params object[] args) { // We need to inject the data item in the globals dictionary to make it available SetDataItem(dataItem); try { return CallFunction(f, args); } finally { ClearDataItem(); } }
internal override bool ProcessEventHandler(string handlerName, Type type, DynamicFunction f) { // Does it look like a handler? if (!handlerName.StartsWith(HandlerPrefix)) return false; string eventName = handlerName.Substring(HandlerPrefix.Length); EventHookupHelper helper = EventHookupHelper.Create(type, eventName, handlerName, f, ScriptVirtualPath); if (helper == null) return false; if (_eventHandlers == null) _eventHandlers = new List<EventHookupHelper>(); _eventHandlers.Add(helper); return true; }
// Call a method internal static object CallMethod(ScriptEngine engine, DynamicFunction f, string defaultVirtualPath, params object[] args) { try { return f.Invoke(engine, args); } catch (Exception e) { if (!ProcessRuntimeException(engine, e, defaultVirtualPath)) throw; } return null; }