/// <summary> /// Registers an instance of the generic type and adds it to the list of registered instances /// </summary> /// <param name="instance">The instance of the generic type to ad</param> /// <param name="variableName">The name of the variable that is accessible from the Javascript side</param> /// <returns>Returns <see langword="this" /> instance to be used for other operations</returns> public virtual ClassBridge <T> AddInstance(T instance, string variableName) { if (instance != null) { if (!Instances.ContainsKey(instance)) { Instances.Add(instance, new List <string>()); if (!InstancesEventsDelegates.ContainsKey(instance)) { InstancesEventsDelegates.Add(instance, new Dictionary <EventInfo, Delegate>()); } foreach ( var info in Events.Where(info => !info.GetAddMethod().IsStatic&& !info.GetRemoveMethod().IsStatic)) { var eventDelegate = InstancesEventsDelegates[instance].FirstOrDefault(pair => pair.Key == info).Value; if (eventDelegate == null) { var eventInvokerReturn = info.EventHandlerType.GetMethod("Invoke").ReturnType; if ((eventInvokerReturn == typeof(void)) || (eventInvokerReturn == typeof(object))) { eventDelegate = ClassBridge.CreateProxyDelegateForEvent(info, instance, RaiseEvent); } else { eventDelegate = ClassBridge.CreateProxyDelegateForEvent(info, instance, (o, s, arg3, arg4) => ClassBridge.NormalizeVariable(RaiseEvent(o, s, arg3, arg4), eventInvokerReturn, false)); } InstancesEventsDelegates[instance].Add(info, eventDelegate); } info.AddEventHandler(instance, eventDelegate); } OnPushJavascript( new FireJavascriptEventArgs(ClassBridge.GenerateInstanceChange(Identification, GlobalPool.GetInstanceId(instance), false))); } } if (!string.IsNullOrWhiteSpace(variableName)) { foreach ( var key in Instances.Keys.Where(key => key != instance) .Where(key => Instances[key].Contains(variableName)) .ToArray()) { RemoveInstance(variableName, key as T); } if (instance != null) { if (!Instances[instance].Contains(variableName)) { Instances[instance].Add(variableName); OnPushJavascript( new FireJavascriptEventArgs(ClassBridge.GenerateInstanceVariable(Identification, GlobalPool.GetInstanceId(instance), variableName))); } } else { OnPushJavascript( new FireJavascriptEventArgs(ClassBridge.GenerateInstanceVariable(Identification, null, variableName))); } } return(this); }
/// <summary> /// Initialize the handler and generates the needed Javascript code /// </summary> /// <param name="bridge">The <see cref="BridgeController" /> object requesting initialization</param> public virtual void Initialize(BridgeController bridge) { var builder = new StringBuilder(); builder.Append(ClassBridge.GenerateNameSpace(GenericType)); builder.Append(ClassBridge.GenerateProxyClass(Identification, false)); lock (Lock) { if (Fields != null) { foreach (var field in Fields) { builder.Append(ClassBridge.GenerateProxyField(Identification, field)); } } if (Methods != null) { if (Properties != null) { foreach (var property in Properties) { var get = property.GetGetMethod(); if ((get != null) && !Methods.ContainsKey(get)) { get = null; } var set = property.GetSetMethod(); if ((set != null) && !Methods.ContainsKey(set)) { set = null; } builder.Append(ClassBridge.GenerateProxyProperty(Identification, property, get, set)); } } foreach ( var method in Methods.Where( pair => (Events == null) || !Events.Any( info => (pair.Key == info.GetAddMethod()) || (pair.Key == info.GetRemoveMethod())))) { builder.Append(ClassBridge.GenerateProxyMethod(Identification, method.Key, method.Value)); } } if (Events != null) { foreach (var info in Events) { var isStatic = info.GetAddMethod().IsStatic || info.GetRemoveMethod().IsStatic; builder.Append(ClassBridge.GenerateProxyField(Identification, info.Name, isStatic, true, new object[0])); builder.Append(ClassBridge.GenerateProxyEventMethods(Identification, info, isStatic)); } } } foreach (var instance in Instances.Where(pair => pair.Key != null).ToArray()) { var instanceId = GlobalPool.GetInstanceId(instance.Key); if (instanceId != null) { builder.AppendLine(ClassBridge.GenerateInstanceChange(Identification, instanceId, false)); foreach (var variableName in instance.Value.Where(s => !string.IsNullOrWhiteSpace(s))) { builder.AppendLine(ClassBridge.GenerateInstanceVariable(Identification, instanceId, variableName)); } } } OnPushJavascript(new FireJavascriptEventArgs(builder.ToString(), bridge)); lock (Lock) { if (SubEnumerations != null) { foreach (var subEnum in SubEnumerations) { subEnum.Initialize(bridge); } } if (SubClasses != null) { foreach (var subClass in SubClasses) { subClass.Initialize(bridge); } } } }