FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/slots/mp_length.cs at master · sdpython/pythonnet · GitHub
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
sdpython
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
slots
/
mp_length.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (77 loc) · 2.86 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
slots
/
mp_length.cs
Copy path
File metadata and controls
85 lines (77 loc) · 2.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Linq
;
using
System
.
Reflection
;
namespace
Python
.
Runtime
.
Slots
{
internal
static
class
mp_length_slot
{
private
static
MethodInfo
_lengthMethod
;
public
static
MethodInfo
Method
{
get
{
if
(
_lengthMethod
!=
null
)
{
return
_lengthMethod
;
}
_lengthMethod
=
typeof
(
mp_length_slot
)
.
GetMethod
(
nameof
(
mp_length_slot
.
mp_length
)
,
BindingFlags
.
Static
|
BindingFlags
.
NonPublic
)
;
Debug
.
Assert
(
_lengthMethod
!=
null
)
;
return
_lengthMethod
;
}
}
public
static
bool
CanAssign
(
Type
clrType
)
{
if
(
typeof
(
ICollection
)
.
IsAssignableFrom
(
clrType
)
)
{
return
true
;
}
if
(
clrType
.
GetInterfaces
(
)
.
Any
(
x
=>
x
.
IsGenericType
&&
x
.
GetGenericTypeDefinition
(
)
==
typeof
(
ICollection
<
>
)
)
)
{
return
true
;
}
if
(
clrType
.
IsInterface
&&
clrType
.
IsGenericType
&&
clrType
.
GetGenericTypeDefinition
(
)
==
typeof
(
ICollection
<
>
)
)
{
return
true
;
}
return
false
;
}
/// <summary>
/// Implements __len__ for classes that implement ICollection
/// (this includes any IList implementer or Array subclass)
/// </summary>
private
static
int
mp_length
(
IntPtr
ob
)
{
var
co
=
ManagedType
.
GetManagedObject
(
ob
)
as
CLRObject
;
if
(
co
==
null
)
{
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
}
// first look for ICollection implementation directly
if
(
co
.
inst
is
ICollection
c
)
{
return
c
.
Count
;
}
Type
clrType
=
co
.
inst
.
GetType
(
)
;
// now look for things that implement ICollection<T> directly (non-explicitly)
PropertyInfo
p
=
clrType
.
GetProperty
(
"Count"
)
;
if
(
p
!=
null
&&
clrType
.
GetInterfaces
(
)
.
Any
(
x
=>
x
.
IsGenericType
&&
x
.
GetGenericTypeDefinition
(
)
==
typeof
(
ICollection
<
>
)
)
)
{
return
(
int
)
p
.
GetValue
(
co
.
inst
,
null
)
;
}
// finally look for things that implement the interface explicitly
var
iface
=
clrType
.
GetInterfaces
(
)
.
FirstOrDefault
(
x
=>
x
.
IsGenericType
&&
x
.
GetGenericTypeDefinition
(
)
==
typeof
(
ICollection
<
>
)
)
;
if
(
iface
!=
null
)
{
p
=
iface
.
GetProperty
(
nameof
(
ICollection
<
int
>
.
Count
)
)
;
return
(
int
)
p
.
GetValue
(
co
.
inst
,
null
)
;
}
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
$
"object of type '
{
clrType
.
Name
}
' has no len()"
)
;
return
-
1
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL