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); }
/// <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); }