Exemplo n.º 1
0
 private static NetToObjectCallables InternalCheckNetToObjectCallables(Type owner)
 {
     lock (syncobj)
     {
         NetToObjectCallables callables;
         if (!netToObjectCallablesTable.TryGetValue(owner, out callables))
         {
             callables = new NetToObjectCallables();
             if (typeof(IInvocationHandler).IsAssignableFrom(owner))
             {
                 ISet <MethodInfo>   filterprops = new HashSet <MethodInfo>();
                 ISet <PropertyInfo> props       = new HashSet <PropertyInfo>();
                 foreach (PropertyInfo pi in owner.GetProperties())
                 {
                     MethodInfo mi = pi.GetGetMethod();
                     if (mi != null)
                     {
                         filterprops.Add(mi);
                     }
                     mi = pi.GetSetMethod();
                     if (mi != null)
                     {
                         filterprops.Add(mi);
                     }
                     props.Add(pi);
                 }
                 Type disposable           = owner.GetInterface(typeof(System.IDisposable).FullName);
                 ISet <MethodInfo> methods = new HashSet <MethodInfo>();
                 foreach (MethodInfo m in owner.GetMethods())
                 {
                     if (disposable != null && m.Name == "Dispose")
                     {
                         continue;
                     }
                     if (filterprops.Contains(m))
                     {
                         continue;
                     }
                     methods.Add(m);
                 }
                 if (methods.Count > 0)
                 {
                     callables.funcs = methods;
                 }
                 if (props.Count > 0)
                 {
                     callables.props = props;
                 }
                 callables.disposable = disposable;
             }
             netToObjectCallablesTable.Add(owner, callables);
         }
         if (callables == null || (callables.funcs == null &&
                                   callables.disposable == null && callables.props == null))
         {
             return(null);
         }
         return(callables);
     }
 }
Exemplo n.º 2
0
        public static IDictionary <MethodInfo, NSJSFunctionCallback> Complier(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type does not allow null values to be entered");
            }
            if (!typeof(IInvocationHandler).IsAssignableFrom(type))
            {
                throw new ArgumentOutOfRangeException("type is not derived from the IInvocationHandler interface");
            }
            IDictionary <MethodInfo, NSJSFunctionCallback> s = new Dictionary <MethodInfo, NSJSFunctionCallback>();
            NetToObjectCallables callables = InternalCheckNetToObjectCallables(type);

            if (callables == null || (callables.funcs == null &&
                                      callables.disposable == null && callables.props == null))
            {
                return(s);
            }
            if (callables.funcs != null)
            {
                foreach (MethodInfo m in callables.funcs)
                {
                    s.Add(m, NSJSPinnedCollection.Pinned(Complier(m)));
                }
            }
            if (callables.props != null)
            {
                foreach (PropertyInfo p in callables.props)
                {
                    MethodInfo gm = p.GetGetMethod();
                    if (gm != null)
                    {
                        s.Add(gm, NSJSPinnedCollection.Pinned(Complier(gm)));
                    }
                    MethodInfo sm = p.GetSetMethod();
                    if (sm != null)
                    {
                        s.Add(sm, NSJSPinnedCollection.Pinned(Complier(sm)));
                    }
                }
            }
            s.Add(typeof(System.IDisposable).GetMethod("Dispose"), FDEFAULTDISPOSE);
            return(s);
        }
Exemplo n.º 3
0
        protected internal static NSJSValue ToObject(NSJSVirtualMachine machine, object obj)
        {
            if (machine == null)
            {
                return(null);
            }
            if (obj == null)
            {
                return(NSJSValue.Null(machine));
            }
            Type       owner     = obj.GetType();
            NSJSObject objective = NSJSObject.New(machine);

            foreach (MemberInfo mi in InternalCheckKeyMembers(owner).Values)
            {
                PropertyInfo pi    = mi as PropertyInfo;
                FieldInfo    fi    = mi as FieldInfo;
                object       value = null;
                Type         clazz = null;
                string       key   = mi.Name;
                if (pi != null)
                {
                    clazz = pi.PropertyType;
                    value = pi.GetValue(obj, null);
                }
                else
                {
                    clazz = fi.FieldType;
                    value = fi.GetValue(obj);
                }
                NSJSValue result = null;
                do
                {
                    if (value == null)
                    {
                        break;
                    }
                    Type element = TypeTool.GetArrayElement(clazz);
                    if (element == null && value is IList)
                    {
                        result = ArrayAuxiliary.ToArray(machine, element, (IList)value);
                    }
                    else if (TypeTool.IsBasicType(clazz) && !TypeTool.IsIPAddress(clazz))
                    {
                        result = value.As(machine);
                    }
                    else
                    {
                        result = ToObject(machine, value);
                    }
                } while (false);
                if (result == null)
                {
                    result = NSJSValue.Null(machine);
                }
                objective.Set(key, result);
            }
            NetToObjectCallables callables = InternalCheckNetToObjectCallables(owner);

            if (callables != null)
            {
                if (callables.funcs != null)
                {
                    foreach (MethodInfo m in callables.funcs)
                    {
                        objective.Set(m.Name, NSJSPinnedCollection.Pinned(Complier(m)));
                    }
                }
                if (callables.props != null)
                {
                    foreach (PropertyInfo p in callables.props)
                    {
                        MethodInfo           gm  = p.GetGetMethod();
                        NSJSFunctionCallback get = null;
                        NSJSFunctionCallback set = null;
                        if (gm != null)
                        {
                            get = NSJSPinnedCollection.Pinned(Complier(gm));
                        }
                        MethodInfo sm = p.GetSetMethod();
                        if (sm != null)
                        {
                            set = NSJSPinnedCollection.Pinned(Complier(sm));
                        }
                        if (set != null || get != null)
                        {
                            objective.DefineProperty(p.Name, get, set);
                        }
                    }
                }
                objective.Set("Dispose", FDEFAULTDISPOSE);
                if (!objective.IsDefined("Close"))
                {
                    objective.Set("Close", FDEFAULTDISPOSE);
                }
                NSJSKeyValueCollection.Set(objective, obj);
            }
            return(objective);
        }