コード例 #1
0
ファイル: PythonTypeOps.cs プロジェクト: slozier/ironpython3
        internal static PythonTypeSlot GetReflectedField(FieldInfo info)
        {
            PythonTypeSlot res;

            NameType nt = NameType.Field;

            if (!PythonBinder.IsExtendedPythonType(info.DeclaringType) &&
                !PythonBinder.IsPythonSupportingType(info.DeclaringType) &&
                !PythonHiddenAttribute.IsHidden(info))
            {
                nt |= NameType.PythonField;
            }

            lock (_fieldCache) {
                if (!_fieldCache.TryGetValue(info, out res))
                {
                    if (nt == NameType.PythonField && info.IsLiteral)
                    {
                        if (info.FieldType == typeof(int))
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                ScriptingRuntimeHelpers.Int32ToObject((int)info.GetRawConstantValue()),
                                true
                                );
                        }
                        else if (info.FieldType == typeof(bool))
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                ScriptingRuntimeHelpers.BooleanToObject((bool)info.GetRawConstantValue()),
                                true
                                );
                        }
                        else
                        {
                            res = new PythonTypeUserDescriptorSlot(
                                info.GetValue(null),
                                true
                                );
                        }
                    }
                    else
                    {
                        res = new ReflectedField(info, nt);
                    }

                    _fieldCache[info] = res;
                }
            }

            return(res);
        }
コード例 #2
0
ファイル: PythonTypeOps.cs プロジェクト: slozier/ironpython3
        /// <summary>
        /// Checks to see if the provided members are always visible for the given type.
        ///
        /// This filters out methods such as GetHashCode and Equals on standard .NET
        /// types that we expose directly as Python types (e.g. object, string, etc...).
        ///
        /// It also filters out the base helper overrides that are added for supporting
        /// super calls on user defined types.
        /// </summary>
        private static bool IsMethodAlwaysVisible(Type /*!*/ type, MemberInfo /*!*/[] /*!*/ methods)
        {
            bool alwaysVisible = true;

            if (PythonBinder.IsPythonType(type))
            {
                // only show methods defined outside of the system types (object, string)
                foreach (MethodInfo mi in methods)
                {
                    if (PythonBinder.IsExtendedPythonType(mi.DeclaringType) ||
                        PythonBinder.IsExtendedPythonType(mi.GetBaseDefinition().DeclaringType) ||
                        PythonHiddenAttribute.IsHidden(mi))
                    {
                        alwaysVisible = false;
                        break;
                    }
                }
            }
            else if (typeof(IPythonObject).IsAssignableFrom(type))
            {
                // check if this is a virtual override helper, if so we
                // may need to filter it out.
                foreach (MethodInfo mi in methods)
                {
                    if (PythonBinder.IsExtendedPythonType(mi.DeclaringType))
                    {
                        alwaysVisible = false;
                        break;
                    }
                }
            }
            else if (type.IsAssignableFrom(typeof(int)))     // GH #52
            // only show methods defined outside of int
            {
                foreach (MethodInfo mi in methods)
                {
                    Debug.Assert(!mi.DeclaringType.IsInterface || typeof(int).GetInterfaces().Contains(mi.DeclaringType));
                    if (PythonBinder.IsPythonSupportingType(mi.DeclaringType) ||
                        mi.DeclaringType.IsInterface ||
                        PythonHiddenAttribute.IsHidden(mi))
                    {
                        alwaysVisible = false;
                        break;
                    }
                }
            }
            return(alwaysVisible);
        }
コード例 #3
0
ファイル: PythonTypeOps.cs プロジェクト: slozier/ironpython3
        internal static ReflectedEvent GetReflectedEvent(EventTracker tracker)
        {
            ReflectedEvent res;

            lock (_eventCache) {
                if (!_eventCache.TryGetValue(tracker, out res))
                {
                    if (PythonBinder.IsExtendedPythonType(tracker.DeclaringType))
                    {
                        _eventCache[tracker] = res = new ReflectedEvent(tracker, true);
                    }
                    else
                    {
                        _eventCache[tracker] = res = new ReflectedEvent(tracker, false);
                    }
                }
            }
            return(res);
        }
コード例 #4
0
ファイル: PythonTypeOps.cs プロジェクト: slozier/ironpython3
        internal static ReflectedGetterSetter GetReflectedProperty(PropertyTracker pt, MemberGroup allProperties, bool privateBinding)
        {
            ReflectedGetterSetter rp;

            lock (_propertyCache) {
                if (_propertyCache.TryGetValue(pt, out rp))
                {
                    return(rp);
                }

                NameType   nt     = NameType.PythonProperty;
                MethodInfo getter = FilterProtectedGetterOrSetter(pt.GetGetMethod(true), privateBinding);
                MethodInfo setter = FilterProtectedGetterOrSetter(pt.GetSetMethod(true), privateBinding);

                if ((getter != null && PythonHiddenAttribute.IsHidden(getter, true)) ||
                    (setter != null && PythonHiddenAttribute.IsHidden(setter, true)))
                {
                    nt = NameType.Property;
                }

                if (pt is ReflectedPropertyTracker rpt)
                {
                    if (PythonBinder.IsExtendedPythonType(pt.DeclaringType) ||
                        PythonHiddenAttribute.IsHidden(rpt.Property, true))
                    {
                        nt = NameType.Property;
                    }

                    if (pt.GetIndexParameters().Length == 0)
                    {
                        List <MethodInfo> getters = new List <MethodInfo>();
                        List <MethodInfo> setters = new List <MethodInfo>();

                        IList <ExtensionPropertyTracker> overriddenProperties = NewTypeMaker.GetOverriddenProperties((getter ?? setter).DeclaringType, pt.Name);
                        foreach (ExtensionPropertyTracker tracker in overriddenProperties)
                        {
                            MethodInfo method = tracker.GetGetMethod(privateBinding);
                            if (method != null)
                            {
                                getters.Add(method);
                            }

                            method = tracker.GetSetMethod(privateBinding);
                            if (method != null)
                            {
                                setters.Add(method);
                            }
                        }

                        foreach (PropertyTracker propTracker in allProperties)
                        {
                            MethodInfo method = propTracker.GetGetMethod(privateBinding);
                            if (method != null)
                            {
                                getters.Add(method);
                            }

                            method = propTracker.GetSetMethod(privateBinding);
                            if (method != null)
                            {
                                setters.Add(method);
                            }
                        }
                        rp = new ReflectedProperty(rpt.Property, getters.ToArray(), setters.ToArray(), nt);
                    }
                    else
                    {
                        rp = new ReflectedIndexer(rpt.Property, NameType.Property, privateBinding);
                    }
                }
                else
                {
                    Debug.Assert(pt is ExtensionPropertyTracker);
                    rp = new ReflectedExtensionProperty(new ExtensionPropertyInfo(pt.DeclaringType, getter ?? setter), nt);
                }

                _propertyCache[pt] = rp;

                return(rp);
            }
        }