-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathIterableDecoder.cs
More file actions
55 lines (45 loc) · 1.78 KB
/
IterableDecoder.cs
File metadata and controls
55 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 IEnumerable
if (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);
}
}
}