using System;namespace Python.Runtime.Codecs{[Obsolete]public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder{public static EnumPyIntCodec Instance { get; } = new EnumPyIntCodec();public bool CanDecode(PyType objectType, Type targetType){return targetType.IsEnum&& objectType.IsSubclass(Runtime.PyLongType);}public bool CanEncode(Type type){return type == typeof(object) || type == typeof(ValueType) || type.IsEnum;}public bool TryDecode<T>(PyObject pyObj, out T? value){value = default;if (!typeof(T).IsEnum) return false;Type etype = Enum.GetUnderlyingType(typeof(T));if (!PyInt.IsIntType(pyObj)) return false;object? result;try{result = pyObj.AsManagedObject(etype);}catch (InvalidCastException){return false;}if (Enum.IsDefined(typeof(T), result) || typeof(T).IsFlagsEnum()){value = (T)Enum.ToObject(typeof(T), result);return true;}return false;}public PyObject? TryEncode(object value){if (value is null) return null;var enumType = value.GetType();if (!enumType.IsEnum) return null;try{return new PyInt(Convert.ToInt64(value));}catch (OverflowException){return new PyInt(Convert.ToUInt64(value));}}private EnumPyIntCodec() { }}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。