Exemplo n.º 1
0
 /// <summary>
 ///     Creates a new instance of this class
 /// </summary>
 /// <exception cref="InvalidGenericTypeException">Indicates that the passed generic type is not a valid class</exception>
 public ClassBridge()
 {
     if (!GenericType.IsClass)
     {
         throw new InvalidGenericTypeException();
     }
     lock (Lock)
     {
         if (Methods == null)
         {
             Methods = GenericType.GetMethods()
                       .Where(
                 info =>
                 info.IsPublic && !info.IsGenericMethod)
                       .ToDictionary(info => info, info => info.GetParameters());
         }
         if (Constructors == null)
         {
             Constructors = GenericType.GetConstructors()
                            .Where(
                 info =>
                 info.IsPublic && !info.IsGenericMethod)
                            .ToDictionary(info => info, info => info.GetParameters());
         }
         if (Properties == null)
         {
             Properties =
                 GenericType.GetProperties()
                 .Where(
                     info =>
                     Methods.ContainsKey(info.GetGetMethod()) ||
                     Methods.ContainsKey(info.GetSetMethod()))
                 .ToList();
         }
         if (Fields == null)
         {
             Fields = GenericType.GetFields().Where(info => info.IsPublic).ToList();
         }
         if (Events == null)
         {
             Events =
                 GenericType.GetEvents()
                 .Where(
                     info =>
                     Methods.ContainsKey(info.GetAddMethod()) &&
                     Methods.ContainsKey(info.GetRemoveMethod()))
                 .ToList();
         }
         foreach (
             var info in
             Events.Where(info => info.GetAddMethod().IsStatic || info.GetRemoveMethod().IsStatic))
         {
             var eventInvokerReturn = info.EventHandlerType.GetMethod("Invoke").ReturnType;
             if ((eventInvokerReturn == typeof(void)) || (eventInvokerReturn == typeof(object)))
             {
                 var del = ClassBridge.CreateProxyDelegateForEvent(info, null, RaiseEvent);
                 StaticEventsDelegates.Add(info, del);
                 info.AddEventHandler(null, del);
             }
             else
             {
                 var del = ClassBridge.CreateProxyDelegateForEvent(info, null,
                                                                   (instance, eventName, isVoid, eventArgs) =>
                                                                   ClassBridge.NormalizeVariable(RaiseEvent(instance, eventName, isVoid, eventArgs),
                                                                                                 eventInvokerReturn, false));
                 StaticEventsDelegates.Add(info, del);
                 info.AddEventHandler(null, del);
             }
         }
         if (SubEnumerations == null)
         {
             SubEnumerations =
                 GenericType.GetNestedTypes(BindingFlags.Public)
                 .Where(type => type.IsEnum)
                 .Select(EnumBridge.FromType)
                 .ToList();
             foreach (var subEnum in SubEnumerations)
             {
                 subEnum.PushJavascript += (sender, args) => OnPushJavascript(args);
             }
         }
         if (SubClasses == null)
         {
             SubClasses =
                 GenericType.GetNestedTypes(BindingFlags.Public)
                 .Where(type => type.IsClass && !typeof(Delegate).IsAssignableFrom(type))
                 .Select(ClassBridge.FromType)
                 .ToList();
             foreach (var subClasses in SubClasses)
             {
                 subClasses.PushJavascript += (sender, args) => OnPushJavascript(args);
             }
         }
     }
 }