コード例 #1
0
        private static PythonDictionary?GetInitializedSlotValues(object obj)
        {
            PythonDictionary   initializedSlotValues = new PythonDictionary();
            IList <PythonType> mro = DynamicHelpers.GetPythonType(obj).ResolutionOrder;

            foreach (object type in mro)
            {
                if (PythonOps.TryGetBoundAttr(type, "__slots__", out object?slots))
                {
                    List <string> slotNames = PythonType.SlotsToList(slots);
                    foreach (string slotName in slotNames)
                    {
                        if (slotName == "__dict__")
                        {
                            continue;
                        }
                        // don't reassign same-named slots from types earlier in the MRO
                        if (initializedSlotValues.__contains__(slotName))
                        {
                            continue;
                        }
                        if (PythonOps.TryGetBoundAttr(obj, slotName, out object?slotValue))
                        {
                            initializedSlotValues[slotName] = slotValue;
                        }
                    }
                }
            }
            if (initializedSlotValues.Count == 0)
            {
                return(null);
            }
            return(initializedSlotValues);
        }