using System;using System.Collections.Generic;namespace Python.Runtime.Codecs{public class SequenceDecoder : IPyObjectDecoder{internal static bool IsSequence(Type targetType){if (!targetType.IsGenericType)return false;return targetType.GetGenericTypeDefinition() == typeof(ICollection<>);}internal static bool IsSequence(PyType objectType){//must implement iterable protocol to fully implement sequence protocolif (!IterableDecoder.IsIterable(objectType)) return false;//returns wheter it implements the sequence protocol//according to python doc this needs to exclude dict subclasses//but I don't know how to look for that given the objectType//rather than the instance.return objectType.HasAttr("__getitem__");}public bool CanDecode(PyType objectType, Type targetType){return IsSequence(objectType) && IsSequence(targetType);}public bool TryDecode<T>(PyObject pyObj, out T value){if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));var elementType = typeof(T).GetGenericArguments()[0];Type collectionType = typeof(CollectionWrappers.SequenceWrapper<>).MakeGenericType(elementType);var instance = Activator.CreateInstance(collectionType, new[] { pyObj });value = (T)instance;return true;}public static SequenceDecoder Instance { get; } = new SequenceDecoder();public static void Register(){PyObjectConversions.RegisterDecoder(Instance);}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。