FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Codecs/SequenceDecoder.cs at master · saroad2/pythonnet · GitHub
saroad2
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
Codecs
/
SequenceDecoder.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (42 loc) · 1.78 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Codecs
/
SequenceDecoder.cs
Copy path
File metadata and controls
52 lines (42 loc) · 1.78 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
using
System
;
using
System
.
Collections
.
Generic
;
namespace
Python
.
Runtime
.
Codecs
{
public
class
SequenceDecoder
:
IPyObjectDecoder
{
internal
static
bool
IsSequence
(
Type
targetType
)
{
if
(
!
targetType
.
IsGenericType
)
return
false
;
return
targetType
.
GetGenericTypeDefinition
(
)
==
typeof
(
ICollection
<
>
)
;
}
internal
static
bool
IsSequence
(
PyObject
objectType
)
{
//must implement iterable protocol to fully implement sequence protocol
if
(
!
IterableDecoder
.
IsIterable
(
objectType
)
)
return
false
;
//returns wheter it implements the sequence protocol
//according to python doc this needs to exclude dict subclasses
//but I don't know how to look for that given the objectType
//rather than the instance.
return
objectType
.
HasAttr
(
"__getitem__"
)
;
}
public
bool
CanDecode
(
PyObject
objectType
,
Type
targetType
)
{
return
IsSequence
(
objectType
)
&&
IsSequence
(
targetType
)
;
}
public
bool
TryDecode
<
T
>
(
PyObject
pyObj
,
out
T
value
)
{
if
(
pyObj
==
null
)
throw
new
ArgumentNullException
(
nameof
(
pyObj
)
)
;
var
elementType
=
typeof
(
T
)
.
GetGenericArguments
(
)
[
0
]
;
Type
collectionType
=
typeof
(
CollectionWrappers
.
SequenceWrapper
<
>
)
.
MakeGenericType
(
elementType
)
;
var
instance
=
Activator
.
CreateInstance
(
collectionType
,
new
[
]
{
pyObj
}
)
;
value
=
(
T
)
instance
;
return
true
;
}
public
static
SequenceDecoder
Instance
{
get
;
}
=
new
SequenceDecoder
(
)
;
public
static
void
Register
(
)
{
PyObjectConversions
.
RegisterDecoder
(
Instance
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL