forked from QuantConnect/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValuePairEnumerableObject.cs
More file actions
51 lines (44 loc) · 1.61 KB
/
KeyValuePairEnumerableObject.cs
File metadata and controls
51 lines (44 loc) · 1.61 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
using System;
using System.Collections.Generic;
namespace Python.Runtime
{
/// <summary>
/// Implements a Python type for managed KeyValuePairEnumerable (dictionaries).
/// This type is essentially the same as a ClassObject, except that it provides
/// sequence semantics to support natural dictionary usage (__contains__ and __len__)
/// from Python.
/// </summary>
internal class KeyValuePairEnumerableObject : LookUpObject
{
internal KeyValuePairEnumerableObject(Type tp) : base(tp)
{
}
internal override bool CanSubclass() => false;
}
public static class KeyValuePairEnumerableObjectExtension
{
public static bool IsKeyValuePairEnumerable(this Type type)
{
var iEnumerableType = typeof(IEnumerable<>);
var keyValuePairType = typeof(KeyValuePair<,>);
var interfaces = type.GetInterfaces();
foreach (var i in interfaces)
{
if (i.IsGenericType &&
i.GetGenericTypeDefinition() == iEnumerableType)
{
var arguments = i.GetGenericArguments();
if (arguments.Length != 1) continue;
var a = arguments[0];
if (a.IsGenericType &&
a.GetGenericTypeDefinition() == keyValuePairType &&
a.GetGenericArguments().Length == 2)
{
return LookUpObject.VerifyMethodRequirements(type);
}
}
}
return false;
}
}
}