Esempio n. 1
0
        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);
        }
        private static bool ShouldIncludeProperty(PythonTypeSlot attrSlot, Attribute[] attributes)
        {
            bool include = true;

            foreach (Attribute attr in attributes)
            {
                ReflectedProperty rp;

                if ((rp = attrSlot as ReflectedProperty) != null && rp.Info != null)
                {
                    include &= rp.Info.IsDefined(attr.GetType(), true);
                }
                else if (attr.GetType() == typeof(BrowsableAttribute))
                {
                    PythonTypeUserDescriptorSlot userSlot = attrSlot as PythonTypeUserDescriptorSlot;
                    if (userSlot == null)
                    {
                        if (!(attrSlot is PythonProperty))
                        {
                            include = false;
                        }
                    }
                    else if (!(userSlot.Value is IPythonObject))
                    {
                        include = false;
                    }
                }
                else
                {
                    // unknown attribute, Python doesn't support attributes, so we
                    // say this doesn't have that attribute.
                    include = false;
                }
            }
            return(include);
        }