using System;using System.Collections.Generic;namespace Python.Runtime.Codecs{public class IterableDecoder : IPyObjectDecoder{internal static bool IsIterable(Type targetType){//if it is a plain IEnumerable, we can decode it using sequence protocol.if (targetType == typeof(System.Collections.IEnumerable))return true;if (!targetType.IsGenericType)return false;return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>);}internal static bool IsIterable(PyType objectType){return objectType.HasAttr("__iter__");}public bool CanDecode(PyType objectType, Type targetType){return IsIterable(objectType) && IsIterable(targetType);}public bool TryDecode<T>(PyObject pyObj, out T value){//first see if T is a plan IEnumerableif (typeof(T) == typeof(System.Collections.IEnumerable)){object enumerable = new CollectionWrappers.IterableWrapper<object>(pyObj);value = (T)enumerable;return true;}var elementType = typeof(T).GetGenericArguments()[0];var collectionType = typeof(CollectionWrappers.IterableWrapper<>).MakeGenericType(elementType);var instance = Activator.CreateInstance(collectionType, new[] { pyObj });value = (T)instance;return true;}public static IterableDecoder Instance { get; } = new IterableDecoder();public static void Register(){PyObjectConversions.RegisterDecoder(Instance);}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。