|
1 | | -usingSystem; |
2 | | -usingSystem.Collections.Generic; |
3 | | - |
4 | | -namespacePython.Runtime.Codecs |
5 | | -{ |
6 | | -publicclassListDecoder:IPyObjectDecoder |
7 | | -{ |
8 | | -privatestaticboolIsList(TypetargetType) |
9 | | -{ |
10 | | -if(!targetType.IsGenericType) |
11 | | -returnfalse; |
12 | | - |
13 | | -returntargetType.GetGenericTypeDefinition()==typeof(IList<>); |
14 | | -} |
15 | | - |
16 | | -privatestaticboolIsList(PyTypeobjectType) |
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 | | -returnPythonReferenceComparer.Instance.Equals(objectType,Runtime.PyListType); |
24 | | -} |
25 | | - |
26 | | -publicboolCanDecode(PyTypeobjectType,TypetargetType) |
27 | | -{ |
28 | | -returnIsList(objectType)&&IsList(targetType); |
29 | | -} |
30 | | - |
31 | | -publicboolTryDecode<T>(PyObjectpyObj,outTvalue) |
32 | | -{ |
33 | | -if(pyObj==null)thrownewArgumentNullException(nameof(pyObj)); |
34 | | - |
35 | | -varelementType=typeof(T).GetGenericArguments()[0]; |
36 | | -TypecollectionType=typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType); |
37 | | - |
38 | | -varinstance=Activator.CreateInstance(collectionType,new[]{pyObj}); |
39 | | -value=(T)instance; |
40 | | -returntrue; |
41 | | -} |
42 | | - |
43 | | -publicstaticListDecoderInstance{get;}=newListDecoder(); |
44 | | - |
45 | | -publicstaticvoidRegister() |
46 | | -{ |
47 | | -PyObjectConversions.RegisterDecoder(Instance); |
48 | | -} |
49 | | -} |
50 | | -} |
| 1 | +usingSystem; |
| 2 | +usingSystem.Collections.Generic; |
| 3 | + |
| 4 | +namespacePython.Runtime.Codecs |
| 5 | +{ |
| 6 | +publicclassListDecoder:IPyObjectDecoder |
| 7 | +{ |
| 8 | +privatestaticboolIsList(TypetargetType) |
| 9 | +{ |
| 10 | +if(!targetType.IsGenericType) |
| 11 | +returnfalse; |
| 12 | + |
| 13 | +returntargetType.GetGenericTypeDefinition()==typeof(IList<>); |
| 14 | +} |
| 15 | + |
| 16 | +privatestaticboolIsList(PyTypeobjectType) |
| 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 | +returnPythonReferenceComparer.Instance.Equals(objectType,Runtime.PyListType); |
| 24 | +} |
| 25 | + |
| 26 | +publicboolCanDecode(PyTypeobjectType,TypetargetType) |
| 27 | +{ |
| 28 | +returnIsList(objectType)&&IsList(targetType); |
| 29 | +} |
| 30 | + |
| 31 | +publicboolTryDecode<T>(PyObjectpyObj,outTvalue) |
| 32 | +{ |
| 33 | +if(pyObj==null)thrownewArgumentNullException(nameof(pyObj)); |
| 34 | + |
| 35 | +varelementType=typeof(T).GetGenericArguments()[0]; |
| 36 | +TypecollectionType=typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType); |
| 37 | + |
| 38 | +varinstance=Activator.CreateInstance(collectionType,new[]{pyObj}); |
| 39 | +value=(T)instance; |
| 40 | +returntrue; |
| 41 | +} |
| 42 | + |
| 43 | +publicstaticListDecoderInstance{get;}=newListDecoder(); |
| 44 | + |
| 45 | +publicstaticvoidRegister() |
| 46 | +{ |
| 47 | +PyObjectConversions.RegisterDecoder(Instance); |
| 48 | +} |
| 49 | +} |
| 50 | +} |