本文整理汇总了C#中PythonDictionary.__len__方法的典型用法代码示例。如果您正苦于以下问题:C# PythonDictionary.__len__方法的具体用法?C# PythonDictionary.__len__怎么用?C# PythonDictionary.__len__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PythonDictionary
的用法示例。
在下文中一共展示了PythonDictionary.__len__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetInitializedSlotValues
/// <summary>
/// Return a dict that maps slot names to slot values, but only include slots that have been assigned to.
/// Looks up slots in base types as well as the current type.
///
/// Sort-of Python equivalent (doesn't look up base slots, while the real code does):
/// return dict([(slot, getattr(self, slot)) for slot in type(self).__slots__ if hasattr(self, slot)])
///
/// Return null if the object has no __slots__, or empty dict if it has __slots__ but none are initialized.
/// </summary>
private static PythonDictionary GetInitializedSlotValues(object obj) {
PythonDictionary initializedSlotValues = new PythonDictionary();
IList<PythonType> mro = DynamicHelpers.GetPythonType(obj).ResolutionOrder;
object slots;
object slotValue;
foreach (object type in mro) {
if (PythonOps.TryGetBoundAttr(type, Symbols.Slots, out slots)) {
List<string> slotNames = NewTypeMaker.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, SymbolTable.StringToId(slotName), out slotValue)) {
initializedSlotValues[slotName] = slotValue;
}
}
}
}
if (initializedSlotValues.__len__() == 0) return null;
return initializedSlotValues;
}