FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Types/MpLengthSlot.cs at v3.0.1 · pythonnet/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pythonnet
/
pythonnet
Public
Notifications
You must be signed in to change notification settings
Fork
780
Star
5.5k
Code
Issues
153
Pull requests
12
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
Types
/
MpLengthSlot.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (61 loc) · 2.32 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Types
/
MpLengthSlot.cs
Copy path
File metadata and controls
68 lines (61 loc) · 2.32 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
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
MpLengthSlot
{
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>
internal
static
nint
impl
(
BorrowedReference
ob
)
{
if
(
ManagedType
.
GetManagedObject
(
ob
)
is
not
CLRObject
co
)
{
Exceptions
.
RaiseTypeError
(
"invalid object"
)
;
return
-
1
;
}
// 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