|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | - |
4 | | -namespace Python.Runtime.Codecs |
5 | | -{ |
6 | | - public class ListDecoder : IPyObjectDecoder |
7 | | - { |
8 | | - private static bool IsList(Type targetType) |
9 | | - { |
10 | | - if (!targetType.IsGenericType) |
11 | | - return false; |
12 | | - |
13 | | - return targetType.GetGenericTypeDefinition() == typeof(IList<>); |
14 | | - } |
15 | | - |
16 | | - private static bool IsList(PyType objectType) |
17 | | - { |
18 | | - //TODO accept any python object that implements the sequence and list protocols |
19 | | - //must implement sequence protocol to fully implement list protocol |
20 | | - //if (!SequenceDecoder.IsSequence(objectType)) return false; |
21 | | - |
22 | | - //returns wheter the type is a list. |
23 | | - return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType); |
24 | | - } |
25 | | - |
26 | | - public bool CanDecode(PyType objectType, Type targetType) |
27 | | - { |
28 | | - return IsList(objectType) && IsList(targetType); |
29 | | - } |
30 | | - |
31 | | - public bool TryDecode<T>(PyObject pyObj, out T value) |
32 | | - { |
33 | | - if (pyObj == null) throw new ArgumentNullException(nameof(pyObj)); |
34 | | - |
35 | | - var elementType = typeof(T).GetGenericArguments()[0]; |
36 | | - Type collectionType = typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType); |
37 | | - |
38 | | - var instance = Activator.CreateInstance(collectionType, new[] { pyObj }); |
39 | | - value = (T)instance; |
40 | | - return true; |
41 | | - } |
42 | | - |
43 | | - public static ListDecoder Instance { get; } = new ListDecoder(); |
44 | | - |
45 | | - public static void Register() |
46 | | - { |
47 | | - PyObjectConversions.RegisterDecoder(Instance); |
48 | | - } |
49 | | - } |
50 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Python.Runtime.Codecs |
| 5 | +{ |
| 6 | + public class ListDecoder : IPyObjectDecoder |
| 7 | + { |
| 8 | + private static bool IsList(Type targetType) |
| 9 | + { |
| 10 | + if (!targetType.IsGenericType) |
| 11 | + return false; |
| 12 | + |
| 13 | + return targetType.GetGenericTypeDefinition() == typeof(IList<>); |
| 14 | + } |
| 15 | + |
| 16 | + private static bool IsList(PyType objectType) |
| 17 | + { |
| 18 | + //TODO accept any python object that implements the sequence and list protocols |
| 19 | + //must implement sequence protocol to fully implement list protocol |
| 20 | + //if (!SequenceDecoder.IsSequence(objectType)) return false; |
| 21 | + |
| 22 | + //returns wheter the type is a list. |
| 23 | + return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType); |
| 24 | + } |
| 25 | + |
| 26 | + public bool CanDecode(PyType objectType, Type targetType) |
| 27 | + { |
| 28 | + return IsList(objectType) && IsList(targetType); |
| 29 | + } |
| 30 | + |
| 31 | + public bool TryDecode<T>(PyObject pyObj, out T value) |
| 32 | + { |
| 33 | + if (pyObj == null) throw new ArgumentNullException(nameof(pyObj)); |
| 34 | + |
| 35 | + var elementType = typeof(T).GetGenericArguments()[0]; |
| 36 | + Type collectionType = typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType); |
| 37 | + |
| 38 | + var instance = Activator.CreateInstance(collectionType, new[] { pyObj }); |
| 39 | + value = (T)instance; |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + public static ListDecoder Instance { get; } = new ListDecoder(); |
| 44 | + |
| 45 | + public static void Register() |
| 46 | + { |
| 47 | + PyObjectConversions.RegisterDecoder(Instance); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments